Gowthami vinoth
Gowthami vinoth

Reputation: 21

What is the Exact use of DisplayFor in mvc razor view?

What is the Exact use of DisplayFor in mvc razor view? Because it will show only string that matches the model property we pass. But in Razor view, we can directly show the property value using model.propertyvalue

What is the difference between below codes,

<span> @Html.DisplayFor(m => m.propertyValue)</span>  

VS

<span> @Model.propertyValue</span>

Upvotes: 1

Views: 206

Answers (1)

quiqs
quiqs

Reputation: 118

When using @Html.DisplayFor(m => m.Property) allows you to take advantage of DataAttributes in your model class. An example would be using [DisplayFormat(NullDisplayText = "whatever you want to display when null")]. I typically have my values display an em-dash when null. Accessing the property directly with @Model.Property will bypass those attributes.

Upvotes: 1

Related Questions