在 ASP.NET MVC 2 中模板化 Html.DisplayFor()
看起来,如果您仅使用 Html.DisplayFor(model => model)
而没有详细信息视图的模板,则生成的标记将如下所示:
<div class="display-label">first name</div>
<div class="display-field">Dan</div>
<div class="display-label">last name</div>
<div class="display-field">M</div>
<div class="display-label">email</div>
<div class="display-field">[email protected]</div>
这具有相当程度的灵活性。如果您为 display-label
和 display-field
创建 CSS 类,您可以做很多事情,但是如果我想将其更改为这样的东西怎么办?
<p>
<span class="display-label">first name</span>:
<span class="display-field">Dan</span>
</p>
<p>
<span class="display-label">last name</span>:
<span class="display-field">M</span>
</p>
<p>
<span class="display-label">email</span>:
<span class="display-field">[email protected]</span>
</p>
请注意,现在属性-值对现在并排显示(而不是在单独的行上),并且每个属性后面都有一个冒号。
有没有办法创建一个自定义模板,当构建详细视图时,该模板将针对每个属性值对重复?
我不是在谈论模型的特定模板(例如,< code>Person 模板)或特定属性的模板(例如,EmailAddress
模板)。我想要一些可以让我描述属性值对外观的东西,然后 DispalyFor()
应该自动为我的模型或视图模型中的每个属性重复该模板。
It appears that if you just use Html.DisplayFor(model => model)
with no templates for a Details view, the resulting markup will look something like this:
<div class="display-label">first name</div>
<div class="display-field">Dan</div>
<div class="display-label">last name</div>
<div class="display-field">M</div>
<div class="display-label">email</div>
<div class="display-field">[email protected]</div>
This has a fair degree of flexibility. If you create CSS classes for display-label
and display-field
, you can do quite a bit, but what if I wanted to change it to something like this?
<p>
<span class="display-label">first name</span>:
<span class="display-field">Dan</span>
</p>
<p>
<span class="display-label">last name</span>:
<span class="display-field">M</span>
</p>
<p>
<span class="display-label">email</span>:
<span class="display-field">[email protected]</span>
</p>
Note that now the attribute-value pairs now appear side-by-side (instead of on separate lines) and there is a colon after each attribute.
Is there any way to create a custom template that will be repeated for each attribute-value pair when a details view is scaffolded?
I'm not talking about a specific template for a model (e.g., a Person
template) or a template for a particular property (e.g., an EmailAddress
template). I want something that just lets me describe how an attribute-value pair looks, then DispalyFor()
should automatically repeat that template for each property in my model or view model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何覆盖
Object
模板,例如参见http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
How about overriding the
Object
template, e.g.See http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html