Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 429
Splendor Avatar asked Apr 12 '13 22:04

Splendor


1 Answers

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)
like image 63
Chtiwi Malek Avatar answered Sep 19 '22 10:09

Chtiwi Malek