Ways to pass information from a controller to a view in ASP.NET MVC

There are three ways to pass information from a controller to a view:

  1. As a dynamic type (using @model dynamic)
  2. As a strongly typed model object.
  3. Using the ViewBag

See the sample list and three different actions for my HomeController:

List<People> peopleList = new List<People>
new People { Name = "Juan",Surname="Lazario",PhoneNumber=0539647},
new People { Name = "Cris",Surname="Tormes",PhoneNumber=0539647},
new People { Name = "Bego",Surname="Calvo",PhoneNumber=0539647},
new People { Name = "Manu",Surname="Martinez",PhoneNumber=0539647}

public ActionResult Dynamic()
return View(peopleList);

public ActionResult StronglyTyped()
return View(peopleList);

public ActionResult ViewBagAction()
ViewBag.People = peopleList;
return View();

1.As a dynamic type (using @model dynamic)

Right-click in the Dynamic() method and Add View

Do not choose any templates.


Change your view page as below:



The result:


2. As a strongly typed model object.

Click right and add view in the StronglyTyped() action. Choose list from the template and Model class as People.


When you click Add, It will create whole view page for us.


Inside the new view template we get intellisense support and our view model is automatically scaffolded. Those are significant advantages and why ASP.NET MVC applications typically use strongly typed views. Strongly-typed view gives you:

  • Automatic scaffolding
  • Intellisense
  • Compile time type checking

3. Using the ViewBag

Another non-strongly typed way we can pass the top blogs into a view template is to use the view bag.  ViewBag is new to MVC 3 and has the advantage that it can be used in combination with a strongly typed model, giving you the advantages for both. ViewBag is useful when you want to pass information not related to the view model and you don’t want to create a view model just to pass the information. For example, you can use it to pass information to your layout template.


Note:I used VS 2015 for this article.

The resource of this article can be reached from here.

#asp-net, #mvc, #viewbag