What is View Data,View Bag and TempData?–MVC options for passing data between current and subsequen


原文:http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-2cplusViewBagplusandplusTem
AS.NET MVC offers three options View Data,VieBag and Tempdata for passing data from controller to view and inxt request.View and View Bag ar and Tempdata performs ditions.Lempdays ponstions.
Simillariaties between View Bag&VieweData:
Helps to mantain data when you move from controller to view.Used to pass data from controller to corerese ponding view.Shott life means value becompes null when redirection occurs.This because their goal is to provide a way to commicate between controles and view.It’s a communication mechans within the server call.24792
ディfference between View Bag&VieweData:
View Data is a dictionary of object s that is derid from View Dictary class and accessible using strigs.View Bag is a dynamic property that tars advantage of the new dynamic feature s in C龚4.0.View Data requires typecasting for complex data type and check for null values to avoid error.View Bag doesn’t require typecasting for complex data type.View Bag&View Data Example:

 Collappse
 |  Copyコード
public ActionResult Index()
{
    ViewBag.Name = "Monjurul Habib";
    return View();
}

 Collappse
 |  Copyコード
public ActionResult Index()
{
    ViewData["Name"] = "Monjurul Habib";
    return View();
} 
In View:

 Collappse
 |  Copyコード
@ViewBag.Name 
@ViewData["Name"] 
TempData:
Tempmpdata is also a dictororororortititititititititititititititititititititititititititititititititititititititititititititititititititime.Th difference isisisththe life cycle of the ooject.Temptadadadadadadadadadadadadakekekekethe the the the thethe the the the ststststststststststststststinininininininininininininininininininininininininininininininininininintitititititititititititititiセレクションit’s in the same HTTP Request.Helps to mantain data when you move from one controller to other controller or from one action to other action.Inother words when you rerect,「Tempdata」helps to mantain data between threredirects.It internally session variablles.Temp data use duristststiningthe current and subsequent request only means itisiswhen sure sure that next rerereest rerererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererereeeeeeeeeeeeeee. generaallyused to store only one time message s like error messages,validation messages.

 Collappse
 |  Copyコード
public ActionResult Index()
{
  var model = new Review()
            {
                Body = "Start",
                Rating=5
            };
    TempData["ModelName"] = model;
    return RedirectToAction("About");
}
<pre><pre lang="cs">public ActionResult About()
{
    var model= TempData["ModelName"];
    return View(model);
}
The last mechange ism the Session which work like the ViewData,ディディディディテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクテクgh the whole life cycle of reglar asp.net mvc request.You can use View Data/Viebag in your child action but be careful that You are not using it to poputlate the unrelated data which can pollut.control.