什么是@ Html.DisplayFor语法? [英] What is the @Html.DisplayFor syntax for?
问题描述
据我所知,剃刀,@Html做了一堆整齐的东西,如生成的链接,输入等HTML。
但我没有得到DisplayFor功能...
我为什么会写:
@ Html.DisplayFor(型号=> model.Title)
当我可以只写:
@ Model.Title
Html.DisplayFor()
将呈现相匹配的属性的类型的DisplayTemplate。
如果它找不到任何,我想它调用的ToString()
。
如果您不知道的显示模板,他们可以放在连接到控制器的视图文件夹内的 DisplayTemplates
文件夹中的部分观点。
例如:
如果您创建一个名为 String.cshtml
查看你的意见文件夹 DisplayTemplates
文件夹中(如首页
或共享
)具有下列code:
@model串@if(string.IsNullOrEmpty(型号)){
<强>空字符串< / STRONG>
}
其他{
@模型
}
然后 @ Html.DisplayFor(型号=> model.Title)
(假设标题
是一个字符串)将使用的模板和显示<强>空字符串< / STRONG>
如果字符串为null,或空
I understand that in Razor, @Html does a bunch of neat things, like generate HTML for links, inputs, etc.
But I don't get the DisplayFor function...
Why would I write:
@Html.DisplayFor(model => model.Title)
when I could just write:
@Model.Title
Html.DisplayFor()
will render the DisplayTemplate that matches the property's type.
If it can't find any, I suppose it invokes .ToString()
.
If you don't know about display templates, they're partial views that can be put in a DisplayTemplates
folder inside the view folder associated to a controller.
Example:
If you create a view named String.cshtml
inside the DisplayTemplates
folder of your views folder (e.g Home
, or Shared
) with the following code:
@model string
@if (string.IsNullOrEmpty(Model)) {
<strong>Null string</strong>
}
else {
@Model
}
Then @Html.DisplayFor(model => model.Title)
(assuming that Title
is a string) will use the template and display <strong>Null string</strong>
if the string is null, or empty.
这篇关于什么是@ Html.DisplayFor语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!