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 controller, my ContentModelView, my view and my Repository :
My Model: two tables : Malades and Consultations
public DbSet Malades { get; set; }
public DbSet Consultations { get; set; }
My Controller:
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
ContentModelView vm = new ContentModelView();
vm.allMalades = _repository.GetMalades(); vm.allConsultations = _repository.GetConsultations(); return View(vm); }
return RedirectToAction("Login", "Account");
}
My ContentViewModel:
public class ContentModelView
{
public List Malades { get; set; }
public List 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)
}
My Repository (repository.cs) :
public List GetMalades()
{ return new List { new Malade () { Id = 1, Name = "something"},
new Malade () { Id = 2, Name = "something"},
new Malade () { Id = 3, Name = "something" },
new Malade () { Id = 4, Name = "something" }
}; }
public List<Consultation> GetConsultations()
{ return new List<Consultation> { new Consultation () { Id = 1, Name = "something"},
new Consultation () { Id = 2, Name = "something"},
new Consultation () { Id = 3, Name = "something" },
new Consultation () { Id = 4, Name = "something" }
}; }
In me repository.cs, we can see that my two tables (Malade and Consultation) are filled manually, It's not that i want. In my projet, the table of my Model (Malade and Consultation) are filled by a form ho are in a view.
i try, this, but he have some mistake. let me show you :
for the table Malade (i do the same thing for "Consultation", so i have the same error)!
public List GetMalades()
{
Malade malade = db.Malades.Single(p => p.UserProfile.UserName == User.Identity.Name);
return new List { new Malade () {
MaladeId = malade.Id, Name = malade.Name
}
}; }.
This this solution that a do, when i filled the first time the table Malades, there are not problem (no problem to save it in the database, no problem to display him in my ViewModel (this single view who can show multiples models)). But, when i filled the table Malades by my form the second time, there is an error (no problem to save it in the database, but it have a problem to display him in my ViewModel) with "Single" ( Malade malade = db.Malades.Single(p => p.UserProfile.UserName == User.Identity.Name);), i think it can show only information when the table are filled one time.
but when i use "FirstOrDefault" instad Single, it don't have problem (to save and to display data), but It don't show me all data in my table Malades when i want to display all data.
Thank you for you help.
To make it clearer, here is my Model, my controller, my ContentModelView, my view and my Repository :
My Model: two tables : Malades and Consultations
public DbSet
public DbSet
My Controller:
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
{
ContentModelView vm = new ContentModelView();
vm.allMalades = _repository.GetMalades(); vm.allConsultations = _repository.GetConsultations(); return View(vm); }
return RedirectToAction("Login", "Account");
}
My ContentViewModel:
public class ContentModelView
{
public List
public List
}
My view:
...
@foreach(var item in Model.Malades)
{
@Html.DisplayNameFor(modelItem => item.Nom)
}
@foreach(var item in Model.Consultations)
{
@Html.DisplayNameFor(modelItem => item.Nombre)
}
My Repository (repository.cs) :
public List
{ return new List
new Malade () { Id = 2, Name = "something"},
new Malade () { Id = 3, Name = "something" },
new Malade () { Id = 4, Name = "something" }
}; }
public List<Consultation> GetConsultations()
{ return new List<Consultation> { new Consultation () { Id = 1, Name = "something"},
new Consultation () { Id = 2, Name = "something"},
new Consultation () { Id = 3, Name = "something" },
new Consultation () { Id = 4, Name = "something" }
}; }
In me repository.cs, we can see that my two tables (Malade and Consultation) are filled manually, It's not that i want. In my projet, the table of my Model (Malade and Consultation) are filled by a form ho are in a view.
i try, this, but he have some mistake. let me show you :
for the table Malade (i do the same thing for "Consultation", so i have the same error)!
public List
{
Malade malade = db.Malades.Single(p => p.UserProfile.UserName == User.Identity.Name);
return new List
MaladeId = malade.Id, Name = malade.Name
}
}; }.
This this solution that a do, when i filled the first time the table Malades, there are not problem (no problem to save it in the database, no problem to display him in my ViewModel (this single view who can show multiples models)). But, when i filled the table Malades by my form the second time, there is an error (no problem to save it in the database, but it have a problem to display him in my ViewModel) with "Single" ( Malade malade = db.Malades.Single(p => p.UserProfile.UserName == User.Identity.Name);), i think it can show only information when the table are filled one time.
but when i use "FirstOrDefault" instad Single, it don't have problem (to save and to display data), but It don't show me all data in my table Malades when i want to display all data.
Thank you for you help.
Replies
Know the answer? Post it — somebody with the same question will find it here.