1

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>
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 4
    Possible duplicate of [When should I use Html.Displayfor in MVC](https://stackoverflow.com/questions/9465376/when-should-i-use-html-displayfor-in-mvc) and [What is the @Html.DisplayFor syntax for?](https://stackoverflow.com/questions/6365633) – adiga May 03 '19 at 07:40

1 Answers1

1

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.

quiqs
  • 108
  • 8