This question relates to an MVC4 project with Entity Framework (database first)
I am passing an (EF) collection of ‘Company’ objects to a view.
I need to show the name of the ‘Primary User’ which is stored as int (foreign key) in each ‘Company’ object.
I am able to get the PrimaryUserID like this :
@foreach (var item in Model)
{
<div>@item.PrimaryUserID</div>
}
but I cannot figure out how to access the PrimaryUser object from the ID?
Is there a way to do this without passing a custom model containing the PrimaryUser object to my view?
Any help would be much appreciated!
//EDIT – this is how I currently pass the collection to my view
public ActionResult Installers(int id = 0)
{
CompanyType installer = db.CompanyTypes.Single(ct => ct.Description == "Installer");
return View(installer.Companies.ToList());
}
You can use the Navigation Property in your model.
Make sure your have included the property in your EF query.
( This is called “eager loading” )
Then just access the property in your markup: