Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6186

how can print value of other table in a model

I am working on asp .net mvc3. My database is SQL Server and I'm using the ado.net database access mechanism.

this is my action in controller:

public ViewResult ProductIndex()
{                
    return View(db.Product.ToList());
}

and this is my ProductIndex.cshtml:

@model IEnumerable<CalcoWOMS.Models.Product>
@foreach (var item in Model) {
    <tr>
        <td>

            @Html.DisplayFor(modelItem => item.ProductCategoryID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductUsageID) 

        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductRangeID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Code)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
}

My problem is that I want to display the value of the "Description" field in the "ProductCategory" Table, not the ProductCategoryID field, in the first line of my ProductIndex.cshtml. How should I modify the controller and view (.cshtml file)?

this is my product table: enter image description here

this is my productCategory table from which I want to access code enter image description here

Upvotes: 0

Views: 546

Answers (1)

David Wick
David Wick

Reputation: 7105

does your model have navigation properties?

if so, this should work (where ProductCategory is the navigation property):

@Html.DisplayFor(modelItem => item.ProductCategory.Description)

Upvotes: 3

Related Questions