Passing Data from Controller to View in ASP.NET MVC: ViewData, ViewBag, and TempData
Master passing data from controller to view in ASP.NET MVC using ViewData, ViewBag, and TempData. This tutorial compares these methods, highlighting their differences, advantages, and disadvantages, with code examples illustrating their usage.
Passing Data from Controller to View in ASP.NET MVC: ViewData, ViewBag, and TempData
Introduction to Data Transfer in ASP.NET MVC
In ASP.NET MVC, transferring data from the controller to the view is crucial for displaying information to the user. ViewData, ViewBag, and TempData are mechanisms for achieving this. This tutorial explains each method and demonstrates their usage with examples.
ASP.NET MVC ViewData
ViewData is a dictionary-like object (derived from ViewDataDictionary
) used to pass data from the controller to the view. Values are accessed using string keys. ViewData is type-safe, requiring explicit type casting to access values. This helps prevent runtime errors associated with null values. ViewData data is only accessible for the current request.
Controller Code (ViewBagExample/ViewBagController.cs)
using System.Collections.Generic;
using System.Web.Mvc;
namespace ViewBagExample.Controllers
{
public class ViewBagController : Controller
{
public ActionResult Index()
{
List<string> courses = new List<string> { "J2SE", "J2EE", "Spring", "Hibernate" };
ViewData["Courses"] = courses;
return View();
}
}
}
View Code (Index.cshtml)
<p>Index</p>
<p>List of Courses:</p>
@{
foreach (var course in ViewData["Courses"] as List<string>)
{
<p>@course</p>
}
}
Output
(A screenshot showing the output displaying the list of courses would be included here.)
ASP.NET MVC ViewBag
ViewBag is a dynamic property similar to ViewData. Introduced in .NET Framework 4.0, it also transfers data from the controller to the view. ViewBag allows dynamic setting and retrieval of values without explicit type casting, making it more convenient to use in some cases.
Controller Code (ViewBagExample/ViewBagController.cs)
using System.Collections.Generic;
using System.Web.Mvc;
namespace ViewBagExample.Controllers
{
public class ViewBagController : Controller
{
public ActionResult Index()
{
List<string> courses = new List<string> { "J2SE", "J2EE", "Spring", "Hibernate" };
ViewBag.Courses = courses;
return View();
}
}
}
View Code (Index.cshtml)
<p>Index</p>
<p>List of Courses:</p>
@{
foreach (var course in ViewBag.Courses)
{
<p>@course</p>
}
}
Output
(A screenshot showing the output displaying the list of courses would be included here.)
ASP.NET MVC TempData
TempData is used to pass data from one request to the next. It's derived from TempDataDictionary
and works similarly to ViewData. Data stored in TempData is only available for the next request. The Keep()
method is used to persist a key across multiple requests.
Controller Code (ViewBagExample/ViewBagController.cs)
using System.Collections.Generic;
using System.Web.Mvc;
namespace ViewBagExample.Controllers
{
public class ViewBagController : Controller
{
public ActionResult Index()
{
List<string> courses = new List<string> { "J2SE", "J2EE", "Spring", "Hibernate" };
TempData["Courses"] = courses;
return View();
}
}
}
View Code (Index.cshtml)
<p>Index</p>
<p>List of Courses:</p>
@{
foreach (var course in TempData["Courses"] as List<string>)
{
<p>@course</p>
}
}
Output
(A screenshot showing the output displaying the list of courses would be included here.)