I am using ASP.NET MVC Entity Framework and I am trying to return two list to one view.
So far I am able to return one list:
public ActionResult Index()
{
return View(db.Data.ToList().Where(model => model.hidden == false).Where(model => model.collection == "Singles"));
}
I also need to return:
db.Data.ToList().Where(model => model.hidden == false).Where(model => model.collection == "Towns")
Create a view model with 2 properties.
Assuming db.Data
returns a collection of type Data
public clas ListViewModel
{
public List<Data> SinglesData { set;get;}
public List<Data> TownsData { set;get;}
}
Now use this
public ActionResult Index()
{
var vm=new ListViewModel();
vm.SinglesData = db.Data
.Where(a=> a.hidden == false && a.collection == "Singles").ToList();
vm.TownsData = db.Data
.Where(b=> b.hidden == false && b.collection == "Towns").ToList();
return View(vm);
}
Now make sure your view is strongly typed to the new view model
@model ListViewModel
<h2>Singles Data </h1>
@foreach(var item in Model.SinglesData)
{
<p>@item.SomeProperty</p>
}
<h2>Towns Data </h1>
@foreach(var item in Model.TownsData)
{
<p>@item.SomeProperty</p>
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.