有没有办法获得 @Html.DisplayFor
如果模型项的值为 null
,则在 View 中显示“NULL”的值?
这是我当前正在处理的“详细信息” View 中的一个项目示例。如果描述的值为 null
,则现在如果不显示任何内容.
<div class="display-field">
@Html.DisplayFor(model => model.Description)
</div>
最佳答案
是的,我建议在您的 codefirst 模型中使用以下数据注释和可为空的日期时间字段:
[Display(Name = "Last connection")]
[DisplayFormat(NullDisplayText = "Never connected")]
public DateTime? last_connection { get; set; }
那么在你看来:
@Html.DisplayFor(x => x.last_connection)
关于asp.net-mvc - 在 ASP.NET MVC DisplayFor Html Helper 中为空值显示 "NULL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981594/