ViewBag
- ViewBag is used to send a small amount of data to the view.
- There may be some situation where you want to transfer some temporary data to view which is not included in the model object.
- The viewBag is a dynamic type property of ControllerBase class.
- It does not retain value when redirection occurs, It is available only for Current Request.
- ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
public class DemoController : Controller
{
IList<Car> CarList = new List<Car>() {
new Car(){ CarName="i10", CarColor="Black",CarMileage=18.2 },
new Car(){ CarName="EON", CarColor="Silver", CarMileage=18.2 },
new Car(){ CarName="Swift", CarColor="Red", CarMileage=18.2},
new Car(){ CarName="Verna", CarColor="Black", CarMileage=18.2},
new Car(){ CarName="Ciaz", CarColor="Silver", CarMileage=18.2}
};
public ActionResult Index()
{
ViewBag.TotalCar = CarList.Count();
return View();
}
}
ViewData
- ViewData is similar to ViewBag, It can also be used to send data from controller to view.
- It is a dictionary, which possesses key-value pairs, where each key must be a string.
- Viewdata last long only for current HTTP request.
public class DemoController : Controller
{
public ActionResult Index()
{
ViewData["Simple"] = "This string is stored in viewData.";
return View();
}
}
To get back the value from viewData write below line in view.
<h4>@ViewData["Simple"]</h4>
TempData
- TempData retain value across subsequent HTTP Request
- It can be used to maintain data between controller actions as well as redirects.
- It internally stores data in session but it gets destroyed earlier than a session.
- ViewBag is used to send a small amount of data to the view.
- There may be some situation where you want to transfer some temporary data to view which is not included in the model object.
- The viewBag is a dynamic type property of ControllerBase class.
- It does not retain value when redirection occurs, It is available only for Current Request.
- ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
public class DemoController : Controller
{
IList<Car> CarList = new List<Car>() {
new Car(){ CarName="i10", CarColor="Black",CarMileage=18.2 },
new Car(){ CarName="EON", CarColor="Silver", CarMileage=18.2 },
new Car(){ CarName="Swift", CarColor="Red", CarMileage=18.2},
new Car(){ CarName="Verna", CarColor="Black", CarMileage=18.2},
new Car(){ CarName="Ciaz", CarColor="Silver", CarMileage=18.2}
};
public ActionResult Index()
{
ViewBag.TotalCar = CarList.Count();
return View();
}
}
ViewData
- ViewData is similar to ViewBag, It can also be used to send data from controller to view.
- It is a dictionary, which possesses key-value pairs, where each key must be a string.
- Viewdata last long only for current HTTP request.
public class DemoController : Controller
{
public ActionResult Index()
{
ViewData["Simple"] = "This string is stored in viewData.";
return View();
}
}
To get back the value from viewData write below line in view.
<h4>@ViewData["Simple"]</h4>
TempData
- TempData retain value across subsequent HTTP Request
- It can be used to maintain data between controller actions as well as redirects.
- It internally stores data in session but it gets destroyed earlier than a session.