I'm learning ASP.NET MVC (and MVC in general) and all the help online I find only demonstrates using a single table, not relations between multiple tables. I'm running a query that I expect should also return associated entities, but it's not and can't figure out what I'm missing. I appreciate your help!
I have the following data:
Ticket
TicketID CompanyID Subject ...
-------- --------- -------
1 1 "stuff"
2 1 "things"
Company
CompanyID Name ...
--------- --------
1 "FredCo"
So every ticket is linked to a specific company. I am trying to create a Details view for Tickets, and I want to display the company name. Here is what I have set up at the moment.
Model
A Ticket
entity and a Company
entity, and there is an association called CompanyTicket
between them. The navigational properties are Ticket.Company and Company.Tickets.
In the mapping details for the association, I have:
Maps to Ticket
Company.CompanyID <-> CompanyID
Ticket.TicketID <-> TicketID
Controller
My TicketController.Details method looks like this:
public ActionResult Details( int id )
{
var tickettoview = ( from m in _db.Ticket
where m.TicketID == id
select m ).First();
return View( tickettoview );
}
Edit: After going through Mark Hamilton's suggestion, I realize the problem is my query. Putting a breakpoint on the return shows that tickettoview
never has Customer populated. So this narrows it down, but I'm still not sure how to populate the Company attribute.
Again, thank you!