views:

26

answers:

1

I have a page with a list of devices. Every device has a detail page. What i'm trying to achieve is get a list of all projects belonging to a device on the device detail page. A device can have several projects, and a project can also be linked to several devices (many-to-many relation). I have a table in my database to link the device and project tables in order to achieve the many-to-many relation. I'm using active record to communicate with my database.

The code for my detail page looks like this:

public ActionResult Details(int id)
    {

        ViewData["Id"] = id;
        return View(Device.Find(id));
    }

My question is: How do I send the list of projects to the device detail view aswell?

A: 

You could add a Projects property to the ViewModel that you use in your Details page and populate it in your controller before your call to render the DetailsView.

I am not sure what your Device.Find(id) returns but if it returns a list project for the device you could do something like this

var projectList = Device.Find(id);

DetailsViewModel viewModel = new DetailsViewModel();

viewModel.Projects = projectList;

viewModel.DeviceName = "Something";

viewModel.Price = 12;

return View("Details", viewModel);

Hector
Actually it returns the detail info from the device table. I could change that and make it show a list of projects. But what I want is displaying data from both tables.
Prd
I would suggest make your viewModel for the Details view have properties for both kind of information (the device + the project list). You can do that inside the Device.Find() or outside of it as in the example that I suggested. I hope this helps.
Hector