简体   繁体   English

在mvc razor视图中,DisplayFor的确切用法是什么?

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

What is the Exact use of DisplayFor in mvc razor view? 在mvc razor视图中, DisplayFor的确切用法是什么? 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 但是在Razor视图中,我们可以使用model.propertyvalue直接显示属性值

What is the difference between below codes, 以下代码有什么区别,

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

VS VS

<span> @Model.propertyValue</span>

When using @Html.DisplayFor(m => m.Property) allows you to take advantage of DataAttributes in your model class. 使用@Html.DisplayFor(m => m.Property) ,您可以利用模型类中的DataAttributes。 An example would be using [DisplayFormat(NullDisplayText = "whatever you want to display when null")] . 一个例子是使用[DisplayFormat(NullDisplayText = "whatever you want to display when null")] I typically have my values display an em-dash when null. 我通常让我的值在null时显示一个em-dash。 Accessing the property directly with @Model.Property will bypass those attributes. 使用@Model.Property直接访问属性将绕过这些属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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