我有兩個實體,它們是從MVC5中實體框架的數據優先方法生成的。 以下是模型mvc中參考實體的顯示名稱
public partial class Cuisine
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Dish> Dishes { get; set; }
}
public partial class Dish
{
public int Id { get; set; }
public string Name { get; set; }
public int CuisineId { get; set; }
public virtual Cuisine Cuisine { get; set; }
}
當我展示它顯示如下
的菜,但我想要的列名作爲名稱和菜名,而不是名稱和姓名。 如何做到這一點? 我想顯示爲名稱當顯示菜單列表和烹飪名稱時顯示在菜單列表中。
代碼爲我CSHTML
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Cuisine.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cuisine.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
爲表格添加CSHTML代碼的問題,請。 –
使用只包含2個屬性的視圖模型,並添加一個'[Display(Name =「Cuisine Name」)]'屬性 –
Hi Vadim Martynov,我添加了cshtml代碼。 – aditya