There are three options in Model View Controller (MVC) for passing data from controller to view. This article attempts to explain the differences among ViewData, ViewBag and TempData with examples. ViewData and ViewBag are similar and TempData performs additional responsibility. The following are the key points on those three objects.
ViewData
- The ViewData is used to move data from controller to view.
- The ViewData is a dictionary of objects that are derived from the "ViewDataDictionary" class and it will be accessible using strings as keys.
- ViewData contains a null value when redirection occurs.
- ViewData requires typecasting for complex data types.
- ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewBag doesn't require typecasting for complex data types.
- ViewBag also contain a null value when redirection occurs.
- ViewData moves data from controller to view.
- Use TempData when you need data to be available for the next request, only. In the next request, it will be there but will be gone after that.
- TempData is used to pass data from the current request to the subsequent request, in other words in case of redirection. That means the value of TempData will not be null.
ViewBag:
- ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewBag doesn't require typecasting for complex data types.
- ViewBag also contain a null value when redirection occurs.
TempData:
- ViewData moves data from controller to view.
- Use TempData when you need data to be available for the next request, only. In the next request, it will be there but will be gone after that.
- TempData is used to pass data from the current request to the subsequent request, in other words in case of redirection. That means the value of TempData will not be null.
Now create a MVC Application to understand what the differences among ViewData, ViewBag and TempData are. The following example is for a Current Request.
Model Code
Right-click on "Model" and add a class with the name Employee.cs and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication.Models
{
public class Employee
{
public string EmpName { get; set; }
}
}
Controller Code:
Right-click on the "Controller" folder and add a controller with the name "EmployeeController" and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication.Models;
namespace MvcApplication.Controllers
{
public class EmployeeController : Controller
{
//
// GET: /Default/
public ActionResult Index()
{
var empName = new Employee
{
EmpName = "Rahul Kumar",
};
ViewBag.Employee = empName; // Defines ViewBag
ViewData["ViewEmployee"] = empName; // Defines ViewData
TempData["TempEmployee"] = empName; // Defines TempData
return View();
}
View Code:
The is the View for showing the user's details. Right-click on "Controller method Index" and add View to the corresponding controller and add the following code:
@model MvcApplication.Models.Employee
@{
var ViewBagTest = (ViewBag.Employee as MvcApplication.Models.Employee).empName;
var ViewDataTest = (ViewData["ViewEmployee"] as MvcApplication.Models.Employee).empName;
var TempDataTest = (TempData["TempEmployee"] as MvcApplication.Models.Employee).empName;
<div>
<h1>@ViewBagTest</h1>
<h2>@ViewDataTest</h2>
<h3>@TempDataTest</h3>
</div>
}
No comments:
Post a Comment