Two tables one view ASP MVC
I need some help. What I am trying to do is show the locations of a device belonging to a specific project.
On the projects page I have a button which goes to the google map. For projects I have a Projects model and controller. I've got 2 tables, a table containing the location of a device and a project table. I want to display all device locations belonging to a project on a google map. The google maps part is working and I managed to show all device locations on 1 map, only problem is I can't seem to link the devices to the project. I need some help on how to achieve this.
In the Location table ive got a foreign key to the projects table. this is a many-to-onerelation. A project can have multiple devices, but a device can only be linked to 1 project. I have defined the relation as follows in the location model:
[BelongsTo("ProjectID")]
public Project proj开发者_如何学编程ect
{
get { return _project; }
set { _project = value; }
}
In the Location controller I use the following code(theres more but for the moment its not relevant):
public ActionResult Index()
{
return View(DeviceLocation.FindAll());
}
This simply lists all locations in the location table. What I need is to list all locations where the ProjectID = the ID of the projects table. I'm coming from PHP/MYSQL and there you would simply join the 2 tables. How do I achieve this with asp mvc?
You should read up on LINQ and how to use it to query your data.
Something like this should help:
public ActionResult Index()
{
return View(DeviceLocation.Where(d=> d.ProjectId == myProjectId));
}
精彩评论