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 Malades { get; set; }
public DbSet 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 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)
}
...
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.
Ramsès Zogning IIPosted Feb 11, 2015, 5:25 AM
Can you think, i can use this (DefaultIfEmpty instad FirstOrDefault) :
Malade malade = db.Malades.DefaultIfEmpty(p => p.UserProfile.UserName == User.Identity.Name);
Thk you for your help
Ramsès Zogning IIPosted Feb 10, 2015, 7:59 AM
i tried with (in my repository) :
public List
{
Malade malade = db.Malades.FirstOrDefault(p => p.UserProfile.UserName == User.Identity.Name);
return new List
MaladeId = malade.Id, Name = malade.Name
}
}; }.
But like we can see, it's not the solution, besause i have, the firts data of my table Malades who is display in my single view.
Thank you for you help.
keyurPosted Feb 10, 2015, 7:50 AM
then you have to use Where and fetch data into List
Ramsès Zogning IIPosted Feb 10, 2015, 7:43 AM
Thank you for your help.
keyurPosted Feb 10, 2015, 7:42 AM
dose that work for you ?
Ramsès Zogning IIPosted Feb 10, 2015, 7:26 AM
keyurPosted Feb 10, 2015, 7:18 AM
So i suggest use FirstOrDefault instad Single
Hope this helps you
Ramsès Zogning IIPosted Feb 10, 2015, 7:13 AM
Ramsès Zogning IIPosted Feb 10, 2015, 7:08 AM
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 (thid 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.
So, Pradeep Shet this is my really problem. Thank you too much for your answer.
keyurPosted Feb 10, 2015, 6:36 AM
You can create ViewModel and put all models which you need in single view.also you can create Partialview in your view which separtes your view file as well.
Hope this will help
Pradeep ShetPosted Feb 10, 2015, 6:13 AM