I have found that many entry level developers get confused by the various kinds of Views in ASP.NET MVC Applications. In this post I am trying to clarify these different types of Views.
The type of ASP.NET MVC View depends on the way the data is passed to it from the Controller. There are three ways data can be passed to Views from a Controller. They are as follows:
The type of ASP.NET MVC View depends on the way the data is passed to it from the Controller. There are three ways data can be passed to Views from a Controller. They are as follows:
Using ViewBag or ViewData
- Using object of the model which is strongly typed
- Using any dynamic type value
On the basis of data transfer mechanism ASP.NET MVC views are categorized as two types:
- Dynamic view
- Strongly typed view
ViewBag allows you to share data dynamically from Controller to View. You can pass data from Controller to View using ViewBag as follows:
public ActionResult Index()
{
ViewBag.ApplicationTitle="Book App";
ViewBag.Genere = from r in GetAuthors()
select r.Genere;
return View();
}
ViewBag is a dynamic object and properties get added to it at run time. There is no predefined property for ViewBag. In a View, the ViewBag property can be accessed using the same property name. You can use ViewBag in a View as follows:

No comments:
Post a Comment