ASP.NET MVC 中的模型绑定嵌套集合

2024-01-06

我正在使用史蒂夫·桑德森的BeginCollectionItem 帮助器 http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/使用 ASP.NET MVC 2 来模型绑定集合(如果项目)。

只要集合项的模型不包含另一个集合,就可以正常工作。

我有一个这样的模型:

-Product
--变体
---包含属性

每当我渲染和建模绑定 Variants 集合时,它都工作得很好。但对于 IncludedAttributes 集合,我无法使用 BeginCollectionItem 帮助器,因为 id 和名称值不会遵循为其父 Variant 生成的 id 和名称值:

<div class="variant">
    <input type="hidden" value="bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126" autocomplete="off" name="Variants.index">
    <input type="hidden" value="0" name="Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].SlotAmount" id="Variants_bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126__SlotAmount">
    <table class="included-attributes">
        <input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id">
        <tr>
            <td>
                <input type="hidden" value="0" name="Variants.IncludedAttributes[c5989db5-b1e1-485b-b09d-a9e50dd1d2cb].Id" id="Variants_IncludedAttributes_c5989db5-b1e1-485b-b09d-a9e50dd1d2cb__Id" class="attribute-id">
            </td>
        </tr>
    </table>
</div>

如果您查看表中第一个隐藏字段的名称,它是 Variants.IncludedAttributes - 它应该是 Variants[bbd4fdd4-fa22-49f9-8a5e-3ff7e2942126].IncludedAttributes[...]...

这是因为当我第二次调用 BeginCollectionItem 时(在 IncludedAttributes 集合上),没有给出有关其父 Variant 的项目索引值的信息。

我的渲染变体的代码如下所示:

<div class="product-variant round-content-box grid_6" data-id="<%: Model.AttributeType.Id %>">
    <h2><%: Model.AttributeType.AttributeTypeName %></h2>
    <div class="box-content">
    <% using (Html.BeginCollectionItem("Variants")) { %>

        <div class="slot-amount">
            <label class="inline" for="slotAmountSelectList"><%: Text.amountOfThisVariant %>:</label>
            <select id="slotAmountSelectList"><option value="1">1</option><option value="2">2</option></select>
        </div>

        <div class="add-values">
            <label class="inline" for="txtProductAttributeSearch"><%: Text.addVariantItems %>:</label>
            <input type="text" id="txtProductAttributeSearch" class="product-attribute-search" /><span><%: Text.or %> <a class="select-from-list-link" href="#select-from-list" data-id="<%: Model.AttributeType.Id %>"><%: Text.selectFromList.ToLowerInvariant() %></a></span>
            <div class="clear"></div>
        </div>
        <%: Html.HiddenFor(m=>m.SlotAmount) %>

        <div class="included-attributes">
            <table>
                <thead>
                    <tr>
                        <th><%: Text.name %></th>
                        <th style="width: 80px;"><%: Text.price %></th>
                        <th><%: Text.shipping %></th>
                        <th style="width: 90px;"><%: Text.image %></th>
                    </tr>
                </thead>
                <tbody>
                    <% for (int i = 0; i < Model.IncludedAttributes.Count; i++) { %>
                        <tr><%: Html.EditorFor(m => m.IncludedAttributes[i]) %></tr>
                    <% } %>
                </tbody>
            </table>
        </div>

    <% } %>
    </div>
</div>

以及渲染包含属性的代码:

<% using (Html.BeginCollectionItem("Variants.IncludedAttributes")) { %>
    <td>
        <%: Model.AttributeName %>
        <%: Html.HiddenFor(m => m.Id, new { @class = "attribute-id" })%>
        <%: Html.HiddenFor(m => m.ProductAttributeTypeId) %>
    </td>
    <td><%: Model.Price.ToCurrencyString() %></td>
    <td><%: Html.DropDownListFor(m => m.RequiredShippingTypeId, AppData.GetShippingTypesSelectListItems(Model.RequiredShippingTypeId)) %></td>
    <td><%: Model.ImageId %></td>
<% } %>

当您使用 MVC 2 和 EditorFor 时,您不需要使用 Steve 的解决方案,我相信这只是 MVC 1 的解决方案。您应该能够执行以下操作:

<% for (int i = 0; i < Model.Variants.Count; i++) { %>
    <%= Html.DisplayFor(m => m.Variants[i].AttributeType.AttributeTypeName) %>
    <% for (int j = 0; j < Model.Variants[i].IncludedAttributes.Count; j++) { %>
        <%= Html.EditorFor(m => m.Variants[i].IncludedAttributes[j]) %>
    <% } %>
<% } %>

请注意,索引 ...[i]...[j]... 的使用很重要,它是 MVC 知道如何正确呈现 Id 和名称的方式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET MVC 中的模型绑定嵌套集合 的相关文章

随机推荐

  • iOS - UILabel 或 UITextView 中的动画文本大小变化?

    在显示文本块的应用程序中 当设备转向横向时 我的字体大小会增加 我不喜欢它如何执行整个动画 然后突然跳到新的尺寸 所以我想在旋转过程中为尺寸变化设置动画 我在某处读到 将这个更改放入UIView动画块不起作用 因为字体属性不可设置动画 那么
  • 谷歌分析数据库[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有人知道 Google Analytics 中的数据是如何组织的吗 很难从大量数据中进行选择 它们执行
  • Laravel Http Facade 中的 PHP Curl 代理选项

    我正在使用 Laravel 的 Http 外观来发出请求 例如 Http withHeaders user agent gt My User agent gt retry 3 500 gt get https example com gt
  • mysql_fetch_array 返回重复数据

    每次我运行 mysql fetch array 时都会返回一个带有重复值的数组e g Array 0 gt 1 row id gt 1 1 gt some text first field gt some text 2 gt some te
  • 将图例添加到 LineCollection 图中

    这是与中给出的答案相关的派生问题根据颜色图设置线条颜色 https stackoverflow com questions 19868548 set line colors according to colormap其中建议了一个很好的解决
  • ASP.NET MVC:路由层次结构 URL

    我该如何为此制定路由 URL 类别 主 子 or 类别 主 sub1 subsub 我希望有 主 子 and 主 子1 子子 作为 CategoryController 的 Index 操作方法中的参数 找到了答案 应在路由路径中使用 ca
  • 使用 Node 'pg' 库连接到 Amazon Redshift

    我尝试使用 pg 库将我的 API 连接到 Redshift 实例 但出现以下错误 Possibly unhandled error SET TIME ZONE is not supported at Connection parseE U
  • Http 304 和缓存控制:无缓存

    我看到对网络服务器的一些调用的以下响应 初始调用 HTTP 1 1 200 OK Date Mon 16 Jan 2012 05 46 49 GMT X Powered By Servlet 2 5 JSP 2 1 Content Type
  • 向 D3 和弦图添加标签

    我是一名菜鸟程序员 所以这对于你们大多数人来说可能是一个简单的任务 此和弦图的标签和 或鼠标悬停文本需要哪些代码行 http mbostock github com d3 ex chord html http mbostock github
  • 在 azure 搜索中索引字典属性

    我有一个具有 type 属性的 DTODictionary
  • TextInputLayout 错误状态下的提示颜色

    根据 Google 的材料指南 https material io guidelines components text fields html text fields layout https material io guidelines
  • 拖动列时显示列标题

    我在中实现代码可分组标题 http www java2s com Code Java Swing Components GroupableGroupHeaderExample htm并尝试启用允许的重新排序 但我有两个问题 1 拖动列时 标
  • 使用自定义插值内核的 MATLAB imresize

    如何使用我的函数作为 MATLAB 中 imresize 函数的插值方法 我阅读了MATLAB关于如何使用自定义函数进行插值方法的帮助 但没有任何明确的示例 我试着为妈妈写一段代码 The imresize命令将默认使用bicubic方法
  • 这个只有一个操作数的 x86-64 addq 指令是什么意思? (摘自CSAPP书籍第三版)

    在下面的说明中 addq 是如何工作的 它只有一个操作数 书上声称它递增 rdx 但 rdx 不在这条指令中 我感到很困惑 这是来自 计算机系统 程序员的视角 一书 第三版 正如 Jester 在评论中指出的那样 这确实是一个错误 我实际上
  • 如何在带引号的表达式中转义扩展路径名扩展模式?

    除了基本的 and 模式 Bash shell 提供了扩展的模式匹配运算符 例如 pattern list 匹配除给定模式之一之外的所有模式 这extglob需要设置 shell 选项才能使用它们 一个例子 mkdir test cd te
  • 根据项目特定情况设置函数默认值 R

    通常 我使用相同的功能设置 我想知道除了在路径中拥有一个本质上是函数包装器的新对象之外 是否还有一种方法来设置默认参数 例如 paste 有它的sep参数设置为空格 我写累了 sep 一遍又一遍 那么有没有办法 暂时 用我选择的默认值替换该
  • 如何从 SVN 的修订版本中删除 1 个文件?

    我的一位同事在 SVN 中签入了一些文件 其中一个文件中有密码 密码已从文件中删除 并签入了新版本 但如果我们查看修订历史记录并转到该修订版本 密码显然仍在存储库中 我们使用 TortoiseSVN 作为客户端 那么如何安全地从 SVN 存
  • Apple iTunes Connect Analytics:归因提供商和营销活动 ID 值传递给应用程序委托

    新的 iTunes Connect 现在具有分析功能 您可以在 URL 中指定提供商 pid ID 和活动 ID cid 例如 您可以将这些值传递到应用程序中以用于各种目的吗 即使经过检查 我也找不到任何表明这一点的内容 didFinish
  • CamelCasePropertyNamesContractResolver 在 MapHttpRoute 之后不工作

    我正在尝试在我的 Net API 项目中实现 JSON 驼峰式大小写 在我的启动类中 我添加了以下几行 config Formatters JsonFormatter SerializerSettings ContractResolver
  • ASP.NET MVC 中的模型绑定嵌套集合

    我正在使用史蒂夫 桑德森的BeginCollectionItem 帮助器 http blog stevensanderson com 2010 01 28 editing a variable length list aspnet mvc