find and solve || findandsolve.com
Please wait.....

ViewData VS ViewBag Vs TempData in C#

Both are(ViewData and ViewBag) used for the sasme purpose to trasger data from controller to view and and TempData performs additional responsibility.ViewBag properties are stored as name and value pairs in the ViewData dictionary.TempData derived from TempDataDictionary class and stored in short lives session.TempData is also a dictionary derived from TempDataDictionary.

Some similarities between ViewBag & ViewData:

  1. Both are  move from controller to view.
  2. Pass data from controller to corresponding view.
  3. Communication is only for server site calling.
  4. Value becomes null when redirection occurs and also call Shoft life. This is because their goal is to provide a way to communicate between controllers and views.

Difference between ViewBag & ViewData:

ViewDataViewBag
 Key-Value Dictionary collectionOnly  type object
Faster than ViewBagslower than ViewData
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above.ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above
It is a dictionary object and property of ControllerBase class.ViewBag is Dynamic property of ControllerBase class.
ViewData is also works grater then .net framework 3.5.ViewBag only works with .net framework 4.0 or grater then 4.0
Type Conversion code is required while enumeratingViewBag is Dynamic use so there is no nee to type conversion while enumerating.
While retrieving, the data it needs to be Type Casted to its original type as the data is stored as objects and it also requires NULL checks while retrieving.While retrieving, there is no need for Type Casting data.

ViewData Example

 //In your Controller Code  
        [HttpGet]
        public ActionResult Index()
        {
            List<string> StaffInfo = new List<string>();
            StaffInfo.Add("Lineka");
            StaffInfo.Add("Demin");
            ViewData["StaffInfo"] = StaffInfo;
            return View();
        }
//In your View Page Code  
<ul>  
    @foreach (var staff in ViewData["StaffInfo"] as List)  
     { 
      <li>@itetm.staff </li>  
     }
</ul>

ViewBag Example

    //In your Controller Code  
        [HttpGet]
        public ActionResult Index()
        {
            List<string> StaffInfo = new List<string>();             StaffInfo.Add("Lineka");
            StaffInfo.Add("Demin");
            ViewBag.StaffInfo = StaffInfo;
            return View();
        }


//In your View Page Code
<ul>
    @foreach (var staff in ViewBag.StaffInfo)
      {
       <li>@itetm.staff </li>
      } 
</ul>

TempData Features

It is derived from TempDataDictionary class and stored in short lives session and it is a string key and object value.TempData also dictionary derived from TempDataDictionary.It is also Key-Value Dictionary collection and also also introduced in MVC1.0 and available in MVC 1.0 and above.TempData keeps the information for the time of an HTTP Request.

  1. Key-Value Dictionary collection.
  2. TempData also works with .net framework 3.5 and above
  3. It is generally used to store only one time messages like error messages.
  4. Data is stored as Object in TempData.
  5. TempData only works during the current and subsequent request.
  6. TempData can be used for passing value from Controller to View and also from Controller to Controller.
  7.  It will not be destroyed on redirection.
        //In your Controller Code  
        public ActionResult Index()
        {
            List<string> StaffInfo = new List<string>();
            StaffInfo.Add("Lineka");
            StaffInfo.Add("Demin");
            TempData["StaffInfo"] = StaffInfo;
            return View();
        }
//In your View Page Code  
<ul>
    @foreach (var staff in TempData["Student"] )
     {
      <li>@item.staff</li>
     }
</ul>  


Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response