@Html.Display() 和 @Html.DisplayText() 之间的区别



@Html.Display(( 和 @Html.DisplayText(( 之间的确切区别是什么?两者都提供相同的输出。我有一个模型类员工,它有一个属性

Name = "my Name".

我正在通过以下方式使用 Html 助手 -

@Html.Display("Name")
@Html.DisplayText("Name")

两种方法的输出都是"我的名字">

您可以检查 Display 和 DisplayText 的源代码。

@Html.DisplayText(propertyName) 将仅输出属性的值(由 ModelMetadataSimpleDisplayText定义(。

@Html.Display(propertyName)提供了更多的选择。如果已从属性类型中提供了DisplayTemplate,则默认情况下它将使用该模板中的 html。您还可以指定用于生成 html 的特定模板名称。此外,还可以使用 Display()additionalViewData 参数将其他信息传递给模板。

在您的情况下,您的属性是string的,并且您尚未为string定义DisplayTemplate,因此Display()使用默认(内置(模板,该模板生成与DisplayText()相同的输出

最新更新