Jan 20, 2014

Different between TempData vs. ViewData vs. ViewBag

1/20/2014 09:50:00 PM

 

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



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

Recent Posts
Popular Articles

 

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

Back To Top