在 ASP.NET MVC 2 中模板化 Html.DisplayFor()

发布于 2024-08-20 12:31:37 字数 1529 浏览 2 评论 0原文

看起来,如果您仅使用 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-labeldisplay-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-08-27 12:31:37

如何覆盖Object模板,例如

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <% if (ViewData.TemplateInfo.TemplateDepth > 3) { %>
        <%= ViewData.ModelMetadata.SimpleDisplayText %>
    <% } else { %>
        <table>
        <% foreach (ModelMetadata prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) { %>
            <% if (prop.HideSurroundingHtml) { %>
                <%= Html.Display(prop.PropertyName) %>
            <% } else { %>
                <tr>
                    <td>
                        <div class="display-label" style="text-align: right;">
                            <%= Html.Label(prop.PropertyName) %>
                        </div>
                    </td>
                    <td>
                        <div class="display-field">
                            <%= Html.Display(prop.PropertyName) %>
                            <%= Html.ValidationMessage(prop.PropertyName, "*") %>
                        </div>
                    </td>
                </tr>
            <% } %>
        <% } %>
        </table>
    <% } %>

参见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.

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <% if (ViewData.TemplateInfo.TemplateDepth > 3) { %>
        <%= ViewData.ModelMetadata.SimpleDisplayText %>
    <% } else { %>
        <table>
        <% foreach (ModelMetadata prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) { %>
            <% if (prop.HideSurroundingHtml) { %>
                <%= Html.Display(prop.PropertyName) %>
            <% } else { %>
                <tr>
                    <td>
                        <div class="display-label" style="text-align: right;">
                            <%= Html.Label(prop.PropertyName) %>
                        </div>
                    </td>
                    <td>
                        <div class="display-field">
                            <%= Html.Display(prop.PropertyName) %>
                            <%= Html.ValidationMessage(prop.PropertyName, "*") %>
                        </div>
                    </td>
                </tr>
            <% } %>
        <% } %>
        </table>
    <% } %>

See http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文