-1

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")
Saadi
  • 2,211
  • 4
  • 21
  • 50
user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

4

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>
}
Shyju
  • 214,206
  • 104
  • 411
  • 497