简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM