How to Display HTML with MVC’s NullDisplayText Attribute

Adding DisplayFormat attribute to model’s property allows you to set formatting options for that property’s value. One of options is NullDisplayText, where you can set what to display if the value is null.

Sometimes you may wish to have NullDisplayText return a piece of HTML. To be able to render this HTML correctly, you need to set HtmlEncode value to false.

[DisplayFormat(NullDisplayText = "<div>My HTML<div>", HtmlEncode = false)]
public int? MyProperty { get; set; }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s