Asp.net-Mvc

在 ASP.NET MVC DisplayFor Html Helper 中為空值顯示“NULL”

  • April 12, 2013

@Html.DisplayFor如果模型項的值為 ,有沒有辦法獲得在視圖中顯示“NULL”的值null

這是我目前正在處理的詳細資訊視圖中的一個項目範例。現在 if 如果 Description 的值為 ,則不顯示任何內容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)

引用自:https://stackoverflow.com/questions/15981594