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

21,263

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)
Share:
21,263
Splendor
Author by

Splendor

Updated on December 18, 2020

Comments

  • Splendor
    Splendor over 3 years

    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>