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 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:
this is my productCategory table from which I want to access code
does your model have navigation properties?
if so, this should work (where ProductCategory
is the navigation property):
@Html.DisplayFor(modelItem => item.ProductCategory.Description)
精彩评论