[英]How can model in Html.DisplayNameFor(model=>model.ID) become Student when @model is IEnumerable<Student>?
[英]How to change the display of @Html.DisplayNameFor(model => model.name)
我做了一个Database Fisrt MVC项目,与此有关。 这是我的一种观点的索引:例如,我想
@ Html.DisplayNameFor(模型=>模型。名称)
显示为“用户”而不是“名称”。
我怎样才能做到这一点?
提前致谢。
@model IEnumerable<dispensario.pacientes>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.cedula)
</th>
<th>
@Html.DisplayNameFor(model => model.carnet)
</th>
<th>
@Html.DisplayNameFor(model => model.tipo_paciente)
</th>
<th>
@Html.DisplayNameFor(model => model.estado)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.cedula)
</td>
<td>
@Html.DisplayFor(modelItem => item.carnet)
</td>
<td>
@Html.DisplayFor(modelItem => item.tipo_paciente)
</td>
<td>
@Html.DisplayFor(modelItem => item.estado)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.idPaciente }) |
@Html.ActionLink("Details", "Details", new { id=item.idPaciente }) |
@Html.ActionLink("Delete", "Delete", new { id=item.idPaciente })
</td>
</tr>
}
</table>
您可以将DisplayName属性添加到您的视图模型。
[DisplayName("User")]
public string name { get; set; }
在您看来:@ Html.DisplayNameFor(model => model.name)
或者,您可以只写“用户”来查看,而不是html helper。
即在您看来:
<th>User</th>
将DisplayName属性添加到属性中以更改模型中的显示名称
[DisplayName("User")]
public string name { get; set; }
我遇到了同样的“问题”,要在Visual Studio Code中正确使用数据注释[DisplayName(“ User”)] ,必须添加using System.ComponentModel。 到我的模型文件。 没有此使用,我将无法成功编译。 希望这个技巧可以帮助某人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.