Thursday, October 22, 2015

ASP.NET MVC DisplayFor Html Helper Set Default Value

Use use DisplayFor Html Helper for displaying our model text in to razor view. It is actually render Span <span> tag in our html document. Sometime our Model returns null values and we required to display the text over null value like this. "Null" , "N/A" ,"$0.00" and so on...
Here is the simplest solution.

In your Model Class (C#). 


Import the namespace:
using System.ComponentModel.DataAnnotations;

Decorate the your property. 

[DisplayFormat(NullDisplayText="N/A")]
public string LabelText { get; set; }

Thats it!. 

No comments:

Post a Comment