boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

ASP.NET MVC: Difference between ViewBag, ViewData & TempData

Updated on     Kisan Patel

ASP.NET MVC offers us three options ViewData, ViewBag & TempData for passing data from controller to view and in next request. ViewData and View Bag are almost similar.

Similarities between View Bag & ViewData:

  1. Helps to maintain data when you move from controller to view.
  2. Used to pass data from controller to corresponding view.
  3. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.

Difference between ViewBag & ViewData:

  • ViewData is a dictionarv of objects that is derived from ViewData Dictionary class and accessible using strings as keys.
  • ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  • ViewData requires typecasting for complex data type and check for null values to avoid error.
  • ViewBag doesn’t require typecasting for complex data type.

ViewData

Controller File Code:

public ActionResult Index() {

}

View File Code:

<p>Data: "@ViewData["mydata"].ToString()"</p>

ViewBag

Controller file Code:

public ActionResult Data() {

}

View File Code:

<p> Data: "@ViewBag.mydata" </p>

Session

You can use session to pass data between Multiple View and Controller

Controller File Code:

public ActionResult Data()

}

View FileCode:

<p>@Session["data"J.ToString()</p>

TempData

TempData is also a dictionary derived from TempDataDictionary class and stored in short lives session and it is a string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTIP Request. This mean only from one page to another. This also work with a 302/303 redirection because it’s in the same HTTP Request. Helps to maintain data when you move from one controller to other controller or from one action to other action. In other words when you redirect, “Tempdata” helps to maintain data between those redirects. It internally uses session variables. Temp data use during the current and subsequent request only means it is use when you are sure that next request will be redirecting to next view. It requires typecasting for complex data. type and check for null values to avoid error. generally used to store only one time messages like error messages, validation messages.

public ActionResult Index()

   var model = new Review{)
   {
      Body = "Start",
      Rating=5
   };

   TempData["ModeIName"] = model;
   return RedirectToAction("About");
}

public ActionResult About() {
   var model= TempData["ModeIName");
   return View(model);
}

ASP.NET MVC

Leave a Reply