使用 ViewModel 强类型视图不会自动生成字段

发布于 2024-09-11 04:41:32 字数 146 浏览 2 评论 0原文

当我创建一个视图并将其直接绑定到一个具有我想要在视图上显示的属性的类时,会自动创建它的字段(文本框等)。但是,当我创建一个 ViewModel 来封装多个带有数据的对象时,这种情况就不会发生。有什么方法可以让它适用于 ViewModel 内的特定对象吗?

谢谢。

When I create a view and bind it directly to one class which has the properties I want to show on the view, the fields (textboxes, etc.) for it are created automatically. But when I create a ViewModel to encapsulate more than one object with data, this doesn't happen. Is there any way to get that working for a specific object which is inside the ViewModel?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡看悲欢离合 2024-09-18 04:41:32

感谢评论者梳理出答案细节。

MVC 的默认 EditorFor“主”模板 Object.ascx 有一个 if 语句来防止这种情况发生。

要更改此行为,您需要将基本 /EditorTemplates/Object.ascx 模板替换为您自己的模板。这是 MVC 中模板的一个很好的复制品:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (Model == null) { %>
    <%= ViewData.ModelMetadata.NullDisplayText %>
<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText %>
<% } else { %>
    <table cellpadding="0" cellspacing="0" border="0">
    <% foreach (var 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;">
                        <%= prop.GetDisplayName() %>
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        <%= Html.Display(prop.PropertyName) %>
                    </div>
                </td>
            </tr>  
        <% } %>
    <% }   %>
    </table>
<% } %>

这一行:

<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>

告诉模板只向下移动对象图的单个级别。只需将 1 替换为 2 或将其完全删除即可更改 MVC 向下钻取的程度。

有关此模板的更多详细信息可以在此处找到:
http: //bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

(伙计,我应该创建一个宏来链接到 Brad Wilson 的东西,我一直这样做);)

Thanks to the commenters for teasing out the answer details.

MVC's default EditorFor "master" template, Object.ascx, has an if statement in place to prevent this from happening.

To change this behavior you need to replace the base /EditorTemplates/Object.ascx template with your own. This is a good replica of the template baked into MVC:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% if (Model == null) { %>
    <%= ViewData.ModelMetadata.NullDisplayText %>
<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    <%= ViewData.ModelMetadata.SimpleDisplayText %>
<% } else { %>
    <table cellpadding="0" cellspacing="0" border="0">
    <% foreach (var 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;">
                        <%= prop.GetDisplayName() %>
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        <%= Html.Display(prop.PropertyName) %>
                    </div>
                </td>
            </tr>  
        <% } %>
    <% }   %>
    </table>
<% } %>

This line:

<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>

tell the template to only go down a single level of your object graph. Simply replace the 1 with 2 or remove it entirely to change how far MVC will drill down.

More details about this template can be found here:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html

( man, I should create a macro to linking to Brad Wilson's stuff, I do it all the time ) ;)

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