11
Answers

Multiple Models in Single View in MVC 4 / 5

Ramsès Zogning II

Ramsès Zogning II

9y
981
1
Hello,

I am trying to create a web application mvc asp dotnet 4 razor.
I would like to put multiple views in a model.
To make it clearer, here is my Model, my eyesight and my controller.
My Model: two tables : Malades and Consultations
public DbSet<Malade> Malades { get; set; }
public DbSet<Consultation> Consultations { get; set; }
My Controller:
public ActionResult Index()
{
   if (User.Identity.IsAuthenticated)
   {
      return View();
   }
   return RedirectToAction("Login", "Account");
}
My ContentViewModel:
public class ViewModelProfil
{
public List<Malade> Malades { get; set; }
public List<Consultation> Consultations { get; set; }
}
 
My view:
...
 @foreach(var item in Model.Malades)
{
   @Html.DisplayNameFor(modelItem => item.Nom)
 
@foreach(var item in Model.Consultations)
{
@Html.DisplayNameFor(modelItem => item.Nombre)
}
 
... 
 
I want to clarify that the data is not manually integrate into the database, but it is a user who filled out a form for the data to be stored in the database.
I did several searches su google, but I fall on tutorials that show how to do it perfectly, but the data are filled manually (Example :  http://www.codeproject.com/Articles/687061/Multiple-Models-in-a-View-in-ASP-NET-MVC-MVC).
 
Thank you for your help.

Answers (11)