0

I'm in ASP.NET using MVC scaffolding. I have a table that has a foreign key in which it has a foreign key I wish to display too. It gets a little confusing.

I have a device table that is linked to a ManufacturerModel table by a foreign key of ManufacturerModelID. The ManufacturerModel table is comprised of the primary key of ManufacurerModelID, a foreign key of ManufacturerID (from a Manufacturer table) and just a Model field. In my device Index screen I would like to display the Device's Manufacturer (from the Manufacturer table) and Model. Currently it will display the model correctly however when I try to add the manufacturer I received the entire ManufacturerModel record including column headers.

In the Device Controller ActionResult for Index() I have the following:

var devices = db.Devices.Include(d => d.DeviceType).Include(d => d.ManufacturerModel);
return View(devices.ToList());

In my Device Index I have the following for Manufacturer and Model

 <th>
     @Html.DisplayNameFor(model => model.ManufacturerModel.ManufacturerID)
 </th>
 <th>
     @Html.DisplayNameFor(model => model.ManufacturerModel.Model)
 </th>
2
  • are you using entity framworke and database first approach? Commented Jun 27, 2016 at 21:03
  • Yes I am using both Commented Jun 27, 2016 at 22:37

2 Answers 2

1

So, If I understood correctly, you have

Device -[ManufacturerModel]-> ManufacturerModel -[ManufacturerID]-> Manufacturer

If this is the case, and the foreign keys are correctly defined, you have a class in your model called Device which has a property called ManufacturerModel which has a property called Manufacturer which contains properties related to the columns of the Manufacturer table. I will asume that you have a Name property and column respectively and you want to display something like

<tr>
    <th>
        @Html.DisplayNameFor(model => model.ManufacturerModel.Manufacturer.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ManufacturerModel.Model)
    </th>
</tr>
<tr>
    <td>@model.ManufacturerModel.Manufacturer.Name</td>
    <td>@model.ManufacturerModel.Model</td>
<tr>

then, when creating the query, you will have to also include (join) the Manufacturertable like

var devices = db.Devices
    .Include(d => d.DeviceType)
    .Include(d => d.ManufacturerModel)
    .Include(d => d.ManufacturerModel.Manufacturer)
Sign up to request clarification or add additional context in comments.

Comments

0

It is because of you tables relations. your Device doesn't have direct relation to ManufacturerModel. Your relation is based on: devices > DeviceType > ManufacturerModel. Try

var devices = db.Devices.Include(d => d.DeviceType).Include(d => d.DeviceType.ManufacturerModel);

2 Comments

It does not allow me to add 'd.DeviceType.ManufacturerModel' as it is not a choice under DeviceType. They are two different tables. If I add 'd.ManufacturerModel.Manufacturer' I received an error on my 'return View(devices.ToList());'
It is just a sample. you should have access to your ManufacturerModel table via devicetype table. just check the object names under devicetype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.