ViewBag vs ViewData vs TempData In ASP.NET MVC

Introduction

In this article, we’ll learn viewdata, ViewBag, and ViewData in ASP.NET MVC application. The ViewData and ViewBag for passing data from the controller to view. and store the dynamic property.

The HomeController Index action method. we’re using the view bag objects Employees property. to pull the Employees that we have stored and then print them.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Employees = new List<string>()
		                    { "James","Robin","Jack" };
        return View();
    }
}

We’re using this ViewBag object to pass data from the controller to view. But remember, the ViewBag object uses the dynamic feature. that uses in C# 4.0. so, this dynamic feature allows an object to have properties added to it.

@{
  ViewBag.Titile = "Employees List";
}

 <h1> Employees List </h1>
 <ul>
    @foreach(string emp in ViewBag.Employees)
	{
	  <li>@emp</li>
	}
 </ul>

The ViewBag and ViewData is a dynamic property. and the downside of these dynamic properties. don’t provide a compile-time error checking. and doesn’t require typecasting for the complex data type.

ViewData

The ViewData is a dictionary of objects. that derive from the ViewDataDictionary class. and accessible using strings as keys.

ViewData uses dynamic properties. like, instead use string keys. so, we use a square bracket there and then we specify a key name. now, if you look at the syntax it’s very much similar to that of a view state, sessions data, and application state.

public class HomeController : Controller
{
  
    public ActionResult Index()
    {
        ViewData["Employees"] = new List<string>()
		                    { "James","Robin","Jack" };
        return View();
    }
}

We can store anything in ViewData. like, a view state or session state or application state. we can store anything and everything into this view data object. so, that’s why a return type is an object. ViewData requires typecasting for the complex data type. and check for null values to avoid an error.

@{
  ViewData["Titile"] = "Employees List";
}

 <h1> Employees List </h1>
 <ul>
    @foreach(string emp in ViewData["Employees"])
	{
	  <li>@emp</li>
	}
 </ul>

Both, ViewData and ViewBag do not provide compile-time error checking. if we misspell it new data of employees. then, we know what happens, get again a null reference exception. so at the compile-time viewdata and ViewBag, they don’t report any errors.

TempData

TempData uses to store temporary data. which use in the after request and discarded after the next request completes. This is useful for one-time messages.

Its uses to the current and subsequent request. when we are sure that the next request will be redirected to another view. It requires typecasting for the complex data type. and check for null values to avoid error and It is generally used to store only one time messages. like error messages, validation messages, etc.

Following example for TempData:

public ActionResult Index()
{
    ViewData["Message"] = "Login failed !";
    return RedirectToAction("Verify");
} 

public ActionResult Verify() 
{     
    var obj= TempData["Message"];     
    return View(obj); 
} 

It helps to maintain data. when you move from one controller to other controller or from one action to other action.

Leave a Reply

Your email address will not be published. Required fields are marked *