Showing posts with label ViewBag. Show all posts
Showing posts with label ViewBag. Show all posts

Jan 20, 2014

Different between TempData vs. ViewData vs. ViewBag

 

TempData


  • TempData is a Dictionary Object
  • It can be used assign value and return

public abstract class ControllerBase : Icontroller
{
public TempDataDictionary TempData { get; set; }
}

  • View is fully loaded it can be used.
  • It’s required typecasting for complex data type and check for null values to avoid error.
  • It is used to store only one time messages like error messages and validation messages
  • TempData is available MVC2,MVC3,MVC4

Syntax:


TempData["cust"] = “Saved SuccessFully”;





ViewData

  • ViewData is a Dictionary object
  • It can be used assign value and return

public abstract class ControllerBase : Icontroller
{
public ViewDataDictionary ViewData { get; set; }
}

o       View Data is used to pass data from controller to corresponding view.
o       It’s life lies only during the current request.
o       If redirection occurs then it’s value becomes null.
o       It’s required typecasting for complex data type and check for null values to avoid error.
o       ViewData is available MVC2,MVC3,MVC4

Syntax:

ViewData["cust"] = cust;


ViewBag *best
o       ViewBag is a dynamic property
o       It can be used return the value.
public dynamic ViewBag { get; }
o       If redirection occurs then it’s value becomes null
o       Wrapper around the ViewData and also used to pass data from controller to      corresponding view.
o       It doesn’t required typecasting for complex data type
o       ViewBag is available MVC4

Syntax:
ViewBag.Title = "Test Application";
ViewBag.Customer = “Test Manufacturing”;

Custom using add Title and customer



Recent Posts
Popular Articles

 

© 2013 MUNISH ORACLE DBA& .Net Developer. All rights resevered. Designed by Templateism

Back To Top