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

204 views Asked by At

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>
1

There are 1 answers

0
quiqs On

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.