Show "NULL" for null values in ASP.NET MVC DisplayFor Html Helper

asp.net-mvc, html-helper, razor

Solution

yes, I would recommend using the following data annotation with a nullable datetime field in your codefirst model :

[Display(Name = "Last connection")]
[DisplayFormat(NullDisplayText = "Never connected")]
public DateTime? last_connection { get; set; }

then in your view :

@Html.DisplayFor(x => x.last_connection)

Problem

Is there a way to get an `@Html.DisplayFor` value to show "NULL" in the view if the value of the model item is `null`? Here's an example of an item in my Details view that I'm working on currently. Right now if displays nothing if the value of the Description is `null`. ``` <div class="display-field"> @Html.DisplayFor(model => model.Description) </div> ```

Original source