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)

Continue reading

Advertisements

#asp-net, #mvc, #viewbag