0

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?

Ethel Patrick
  • 885
  • 7
  • 18
  • 38
  • generally when I want to display information about something I do an ajax call back to the controller to get a partial view with that information that I show in a popup. would something like that be an option for you? – Matt Bodily Aug 15 '16 at 19:55
  • Do you have an example or somewhere I could go to see what you a describing? – Ethel Patrick Aug 15 '16 at 20:34
  • there are examples all over. http://stackoverflow.com/questions/28613630/how-do-i-refresh-a-partial-view-every-3-seconds-in-mvc-4/28615705#28615705 is a way to load a partial with a function call. this will hopefully give you the basis of what you need – Matt Bodily Aug 15 '16 at 20:57
  • What is the relationship between these entities? Why do you have 3 separate lists in your view model when you appear to be wanting to display the Devices for each Location (i.e. nested loops) –  Aug 15 '16 at 23:07
  • Location and Device are a many to many relationship broken up by DeviceLocation – Ethel Patrick Aug 16 '16 at 14:22

0 Answers0