Click here to Skip to main content
15,883,750 members
Articles / Programming Languages / C++

Passing data from Controller to View in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
3.58/5 (13 votes)
16 Jun 2014CPOL3 min read 118.2K   13   2
ASP.NET MVC is a framework that facilitates building web applications based on MVC (Model-View-Controller) design pattern. Request coming from client reaches the Controller through URL Rewriting Module. Controller decides which model to use in order to fulfill the request. Further passing the Model

ASP.NET MVC is a framework that facilitates building web applications based on MVC (Model-View-Controller) design pattern. Request coming from client reaches the Controller through URL Rewriting Module. Controller decides which model to use in order to fulfill the request. Further passing the Model data to View which then transforms the Model data and renders response to client as shown in following basic level request flow diagram.

ASP.NET MVC Request Flow

In this ASP.NET MVC Tutorial, we will discuss and implement different options to pass data from ASP.NET MVC Controller to View. Following are the available options to pass data from a Controller to View in ASP.NET MVC:

  • ViewBag
  • ViewData
  • TempData

If we want to maintain state between a Controller and corresponding View- ViewData and ViewBag are the available options but both of these options are limited to a single server call (meaning it’s value will be null if a redirect occurs). But if we need to maintain state from one Controller to another (redirect case), then TempData is the other available option.

It’s common that initially it might be a bit difficult for a ASP.NET WebForms developer to digest above flow and need for options to pass data from Controller to View. Because in WebForms approach, Controller and View are tightly coupled to each other. Please follow the link for a detailed comparison of the differences between ASP.NET WebForms and ASP.NET MVC here.

For the purpose of implementation, we will take earlier ASP.NET MVC tutorial on this blog as base and implement with different options. If you haven’t gone through the article, please read “Building your first ASP.NET MVC application in 4 simple steps” first.

ViewBag Example

As we discussed earlier that ViewBag and ViewData serves the same purpose but ViewBag is basically a dynamic property (a new C# 4.0 feature) having advantage that it doesn’t have typecasting and null checks.

So, In order to pass data from Controller to View using ViewBag, we will modify our EmployeeController code as follows:

C#
public class EmployeeController : Controller
{
          // GET: /Employee/
         public ActionResult Index()
        {
                    ViewBag.EmployeeName = "Muhammad Hamza";
                    ViewBag.Company = "Web Development Company";
                    ViewBag.Address = "Dubai, United Arab Emirates";
                    return View();
        }
 }

And to get Employee details passed from Controller using ViewBag, View code will be as follows:

<body>
  <div>
        <h1>Employee (ViewBag Data Example)</h1>
        <div>
                     <b>Employee Name:</b> @ViewBag.EmployeeName<br />
                    <b>Company Name:</b> @ViewBag.Company<br />
                    <b>Address:</b> @ViewBag.Address<br />
        </div>
  </div>
</body>

In order to see the above changes in action run the solution, we will find the following output.

ViewBag Example

ViewData Example

As compared to ViewBag, ViewData is a dictionary object which requires typecasting as well as null checks. Same above implementation using ViewData can be achieved as follows:

C#
public class EmployeeController : Controller
{
          // GET: /Employee/
         public ActionResult Index()
        {
                    ViewData["EmployeeName"] = "Muhammad Hamza";
                    ViewData["Company"] = "Web Development Company";
                    ViewData["Address"] = "Dubai, United Arab Emirates";
                    return View();
        }
 }

And to get Employee details passed from Controller using ViewBag, View code will be as follows:

<body>
  <div>
        <h1>Employee (ViewBag Data Example)</h1>
        <div>
                     <b>Employee Name:</b> @ViewData["EmployeeName"]<br />
                    <b>Company Name:</b> @ViewData["Company"]<br />
                    <b>Address:</b> @ViewData["Address"]<br />
        </div>
  </div>
</body>

Run the application to view the following output.

ViewData Example

Hopefully, this ASP.NET MVC Tutorial will provide reader with a better understanding of passing data from Controller to View in ASP.NET MVC using ViewBag and ViewData. Please follow the link for detailed understanding about Using TempData in ASP.NET MVC.

Other Related Articles

The post Passing data from Controller to View in ASP.NET MVC appeared first on Web Development Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
QuestionNice Article Pin
a.kamal82-Dec-14 0:49
a.kamal82-Dec-14 0:49 
AnswerRe: Nice Article Pin
Imran Abdul Ghani2-Dec-14 0:55
Imran Abdul Ghani2-Dec-14 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.