In my Detail View I would like to show a List from another Model. -- I have Locations that can have Devices assigned to them. I would like to see the Details of a Location and see a list of Devices assigned to it. I have been struggling with this for sometime and have tried a number of approaches. It just seems that there should simple solution for this however I just come up against the wall on building the list. For my latest approach I have created my own ViewModel.
ViewModel - DeviceLocationIndex Model
public class DeviceLocationIndex
{
public IEnumerable<Device> Devices { get; set; }
public IEnumerable<Location> Locations { get; set; }
public IEnumerable<DeviceLocation> DeviceLocations { get; set; }
public List<Device> locationDevices { get; set; }
}
I created a partial view - _DeviceIndex to be put at the bottom of my Location Detail View.
<div>
@Html.Partial("_DeviceIndex", Model.Devices)
</div>
In my LocationController I have
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Location location = db.Locations.Find(id);
if (location == null)
{
return HttpNotFound();
}
var viewModel = new DeviceLocationIndex();
viewModel.Locations = db.Locations
.Include(x => x.DeviceLocations.Select(d => d.Device))
.Where(x => x.LocationID == id) ;
var locationID = location.LocationID;
I should explain there are three tables: location, DeviceLocation, and Device. This next piece of code shows me the records in the DeviceLocation Table (This location has these devices).
ViewBag.LocationID = locationID;
viewModel.DeviceLocations = viewModel.Locations.Where(
x => x.LocationID == locationID).Single().DeviceLocations;
int count = viewModel.DeviceLocations.Count();
while (count != 0)
{
}
I am at a loss at this point. How do I build a Device list from what I know? Then how do you pass the information needed for the Detail portion and the List portion to the View together?