开发者

how can print value of other table in a model

开发者 https://www.devze.com 2023-03-14 02:44 出处:网络
I am working on asp .net mvc3. My database is SQL Server and I\'m using the ado.net database access mechanism.

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 Produc开发者_如何学JAVAtIndex()
{                
    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:

how can print value of other table in a model

this is my productCategory table from which I want to access code

how can print value of other table in a model


does your model have navigation properties?

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消