MVC 6 Viewbag and Viewdata
MVC 6 Viewbag and Viewdata
ViewBag
Note:
ViewBag only transfers data from controller to view, not visa-versa.
ViewBag values will be null if redirection occurs.
The following example demonstrates how to transfer data from
controller to view using ViewBag.
return View();
}
}
}
In the above example, we want to display the total number of
students in a view for the demo. So, we have attached the
TotalStudents property to the ViewBag and assigned the student
count using studentList.Count() .
Points to Remember :
1. ViewBag transfers data from the controller to the view, ideally
temporary data which in not included in a model.
2. ViewBag is a dynamic property that takes advantage of the new
dynamic features in C# 4.0
3. You can assign any number of propertes and values to ViewBag
4. The ViewBag's life only lasts during the current http request.
ViewBag values will be null if redirection occurs.
5. ViewBag is actually a wrapper around ViewData.
ViewData
Note:
ViewData only transfers data from controller to view, not vice-
versa. It is valid only during the current request.
The following example demonstrates how to transfer data from
controller to view using ViewData.
ViewData["students"] = studentList;
return View();
}
In the above example, we have added a student list with the key
"students" in the ViewData dictionary. So now, the student list can
be accessed in a view as shown below.
return View();
}
ViewData and ViewBag both use the same dictionary internally. So
you cannot have ViewData Key matches with the property name of
ViewBag, otherwise it will throw a runtime exception.
return View();
}
In the Index action of your controller create a Product object and assign it to a
ViewData dictionary object as follows.
1
2 public ActionResult Index()
3 {
Product cap = new Product
4 {
5 Id = 1,
6 Name = "Golf cap",
7 Description = "A nice cap to play golf",
price = 10m
8 };
9
10 ViewData["MyProduct"] = cap;
11
12 return View();
13 }
14
Change the Index View file contents as follows, build and run your application.
@using MvcPassDataToView.Models
1
@{
2 ViewBag.Title = "Index";
}
3
4
<h2>Index</h2>
5
6
@{
7
var prod = (Product)ViewData["MyProduct"];
8
}
9
10
<h1>Product</h1>
11
<h3>ID: @prod.Id</h3>
12 <h3>Name: @prod.Name </h3>
13 <h3>Description: @prod.Description</h3>
14 <h3>Price: @prod.price</h3>
Points to Remember :
1. ViewData transfers data from the Controller to View, not vice-versa.
2. ViewData is derived from ViewDataDictionary which is a dictionary
type.
3. ViewData's life only lasts during current http request. ViewData
values will be cleared if redirection occurs.
4. ViewData value must be type cast before use.
TempData
1. TempData is derived from the TempDataDictionary class and is
basically a Dictionary object i.e. Keys and Values where Keys are
String while Values will be objects.
2. Data is stored as Object in TempData.
3. 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.
4. TempData can be used for passing value from Controller to
View and also from Controller to Controller.
5. TempData is available for Current and Subsequent Requests. It
will not be destroyed on redirection.
Example
In the below example, a string value is set in the TempData object
in Controller and it is redirected to another Controller and finally it
is displayed in View.
First Controller
public class FirstController : Controller
{
// GET: First
public ActionResult Index()
{
TempData["Message"] = "Hello MVC!";
return new RedirectResult(@"~\Second\");
}
}
Second Controller
public class SecondController : Controller
{
// GET: Second
public ActionResult Index()
{
return View();
}
}