1.

What is ViewData, ViewBag and TempData?

Answer»

ViewData:ViewData is used when we want to transfer the data from controller to view it as a dictionary which is used to transfer the data in the form of key-value pair where key is always string, we can transfer the data from the controller to view but it is not possible to transfer the data from view to controller using ViewData.

Eg, 

ActionResult Index() { IList&LT;Student> studentList = new List<Student>(); studentList.Add(new Student(){ StudentName = "Bill" }); studentList.Add(new Student(){ StudentName = "Steve" }); studentList.Add(new Student(){ StudentName = "Ram" }); ViewData["STUDENTS"] = studentList; return View(); }

Access ViewData in a Razor View

<ul> @foreach (var std in ViewData["students"] as IList<Student>) { <li> @std.StudentName </li> } </ul>

Add KeyValuePair in ViewData

public ActionResult Index() { ViewData.Add("Id", 1); ViewData.Add(new KeyValuePair<string, object>("Name", "Bill")); ViewData.Add(new KeyValuePair<string, object>("Age", 20)); return View(); }

Viewdata request is last till the current web request, it the cleared the view data when there is the redirection, before using the viewdata we need to cast it. We can use the temp data in MVC 1.0 and its above version.it REQUIRES the type conversion while we are enumerating. It will throw the error when there is the redirection of a request.

ViewBag : ViewBag is used to when we want to transfer the small amount of data from Controller to view or we can say when we want to transfer temporary data (which is not included in model) from the controller to the view.its derived from the controller base Class which is a base class for all the controller,it attaches the name property with dot notation and assign the string value in it and using razor SYNTAX @ to get the server side code,

You can assign any number of properties and values to ViewBag. What so ever time you will assign the values to the viewbag it will consider the last value defined in the property.

namespace MVC_BasicTutorials.Controllers { public class StudentController : Controller { IList<Student> studentList = new List<Student>() { new Student() { StudentID=1, StudentName="Steve", Age = 21 }, new Student() { StudentID=2, StudentName="Bill", Age = 25 }, new Student() { StudentID=3, StudentName="Ram", Age = 20 }, new Student() { StudentID=4, StudentName="Ron", Age = 31 }, new Student() { StudentID=5, StudentName="Rob", Age = 19 } }; // GET: Student public ActionResult Index() { ViewBag.TotalStudents = studentList.Count(); return View(); } } }

On view page we can access the data by

<label>Total Students:</label> @ViewBag.TotalStudents

TempData: ASP.NET MVC can be used to store temporary data which can be used in the subsequent request.it is very useful when we want to transfer the data from one action to another action or from one controller to another controller, the data is passed in the form of key-value pair

public class HomeController : Controller

{ // GET: Student public HomeController() { } public ActionResult Index() { TempData["name"] = "Test data"; TempData["age"] = 30; return View(); } public ActionResult About() { string userName; int userAge; if(TempData.ContainsKey("name")) userName=TempData["name"].ToString(); if(TempData.ContainsKey("age")) userAge = int.Parse(TempData["age"].ToString()); return View(); } }

Life span for temp data is very small. Data in TempData can persist only upto next view or upto next controller action. If we want to take data in TempData to successive requests then we need to call a method: TempData.Keep(), to keep the data persists for next request.



Discussion

No Comment Found