Thursday, January 29, 2015

View Method, ViewData and TempData in MVC5


Views provide the user interface as response to an action. In general, when the user hits a URL, it gets mapped to a controller action and the controller action then returns a view which contains the HTML supposed to be rendered by the browser.

When we look at the default Home Controller's Index() method, we see that it simply returns the result of the View() method.

public class HomeController : Controller
        public ActionResult Index()
            return View();

Let's take a look at what happens under the hood.

Controller.View() Method

The View() method creates an object of type ViewResult and returns it. This object is responsible for rendering the View. Inside the View() method in controller class, we just simply set the properties like: ViewName, masterName and model. The View method in controller class has multiple overloads for different combinations of the aforementioned three properties. For any of the overloads, if some property is not set, it is simply set to null
i)   View()
ii)  View(object model)
iii) View(string viewName)
iv) View(string viewName, object model)
v)  View(string viewName, string masterName)
vi) View(string viewName, string masterName, object model)

viewName is used to pass the name of the view. model is used to pass the viewmodel i.e. data to be rendered in the view. masterName is used to specify the name of the master page to be rendered.
Other than these, there are 2 more overloads of the View method which take the IView object and model:
vii) View(IView view)
viii) View(IView view, object model)

view parameter is used to specify the view to be rendered.
So once these properties are set inside the ViewResult object and returned, the ASP.NET MVC framework calls the ExecuteResult() method of the ViewResult object in order to render it. I have already talked about ExecuteResult() method in a previous blog post.

Default View Path

About ViewPath, I want to mention one thing i.e. If no view name is passed, by default the MVC framework searches for a view at Views/[Controller]/[Action].cshtml. So for the HomeController's Index Action, we get /Views/Home/Index.cshtml view.

ViewBag, ViewData & ViewDataDictionary

ViewData is of type ViewDataDictionary which is a specialized dictionary. It is used to pass data between controller and view. Inside the controller action method, we can pass value to the view like this:

ViewData["color"] = "red";

And In the View, we can use that value like this:


Similarly, we can pass the data in reverse direction as well i.e. from view to controller action.

ViewBag is a dynamic wrapper around ViewData. It's just syntactical sugar which allows us to access the same data like this:


They both point to the same data in terms of memory. So we can even set value for ViewData["color"] and access it using ViewBag.color, if we want.


Our discussion won't be complete if we talk about ViewData and don't talk about TempData. This is similar to ViewData in the sense that its also used to send data between controller and view but the key difference is, it is used to send data between current and next HTTP Requests. When we redirect as part of an action method result, a new HTTP request is generated from the browser to the redirected route. In such a case, if we have anything set in the ViewData, it won't be available to us because ViewData is accessible only per request. In such a scenario, we can use TempData. Syntactically, its similar to ViewData.

public ActionResult Index()
            TempData["mycolor"] = "black";
            return Redirect("Home/About");


Overall, Controller's View method's various overloads give us control which view to render and the viewmodel to be passed in. MVC provides us with some default convention to follow for organizing our views in the directory but it also allows us to easily override those if needed. Also, in this post we discussed the underlying structure of ViewData and when to use ViewData and TempData.

For future updates to my weekly blog, please subscribe to the blog and follow me on Twitter.