开发者

Detail view list from multiple tables(ASP MVC)

开发者 https://www.devze.com 2023-01-18 21:01 出处:网络
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 sever

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 开发者_Go百科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?


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消