简体   繁体   English

嵌套列表模型绑定和HTML帮助器

[英]nested list model binding and HTML helpers

I'm trying to create an editor in ASP.NET MVC 2 for a list of check boxes. 我试图在ASP.NET MVC 2中为复选框列表创建一个编辑器。 The collection of these checkboxes is based off of a database table. 这些复选框的收集基于数据库表。 I'm having trouble getting the names to come out correctly so that the values post... 我无法正确显示名称,以便发布值...

ViewModels: 的ViewModels:

public class EAF
{
    // other properties
    public IEnumerable<ProductViewModel> _products { get; set; }
}

public class ProductViewModel
{
    public int ProductPK { get; set; }
    public string ProductName { get; set; }
    public bool IsSelected { get; set; }
}

Main View: 主视图:

<%: Html.EditorFor(m => m._products, "ProductListTemplate") %>

Editor Template (list): 编辑器模板(列表):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<EmployeeAccessForm.Models.ProductViewModel>>" %>

<fieldset>
<legend>REQUEST FOR:  (select item(s) to display access form)</legend>
<table>
    <tr>
<% for (int i = 0; i < Model.Count(); i++) { %>
    <td>
    <%: Html.EditorFor(model => model[i], "ProductCheckBox") %>
    </td>
    <% if (i+1 % 5 == 0) { %>
        </tr>
        <tr>
    <% } %>
<% } %>
    </tr>
</table>
</fieldset>

Editor Template (item): 编辑器模板(项目):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EmployeeAccessForm.Models.ProductViewModel>" %>

<%: Html.HiddenFor(model => model.ProductPK)%>
<%: Html.CheckBoxFor(model => model.IsSelected)%>
<%: Html.DisplayTextFor(model => model.ProductName)%>

I get a table of checkboxes as expected, but when I view the source, the inputs all have a name like _products.[0].ProductPK where I need it to be _products[0].ProductPK (without the first dot). 我得到了预期的复选框表,但是当我查看源代码时,所有输入都具有_products.[0].ProductPK类的名称,我需要将其_products[0].ProductPK_products[0].ProductPK (不带第一个点)。 Is there a way using the helpers to get the names to come out like the latter? 有没有办法使用助手来使名字像后者一样出来? I can do it by writing out the HTML explicitly but wondered if I could still use the helpers and get the right naming. 我可以通过明确地写出HTML来做到这一点,但想知道我是否仍然可以使用辅助程序并获得正确的命名。

The problem is coming from using an editor template for the list. 问题来自使用列表的编辑器模板。 Even though you didn't post it, I'm assuming in your main view you have something like: 即使您没有发布它,但我假设在您的主视图中您有类似以下内容:

<%: Html.EditorFor(m => m._products, "ProductListTemplate") %>

This results in a prefix being added of _products . 这将导致在_products中添加前缀。 Then in your list template, the field names there are being bound in the form of [0].ProductPK . 然后,在列表模板中,以[0].ProductPK的形式绑定字段名称。 So the result is _products.[0].ProductPK . 因此结果是_products.[0].ProductPK

Instead in your main view you should be doing: 相反,应在主视图中执行以下操作:

<fieldset>
<legend>REQUEST FOR:  (select item(s) to display access form)</legend>
<table>
    <tr>
<% for (int i = 0; i < Model.Count(); i++) { %>
    <td>
    <%: Html.EditorFor(model => model._products[i], "ProductCheckBox") %>
    </td>
    <% if (i+1 % 5 == 0) { %>
        </tr>
        <tr>
    <% } %>
<% } %>
    </tr>
</table>
</fieldset>

In other words, remove that middle layer that's screwing up the field names. 换句话说,请删除破坏字段名称的中间层。 Since the expression being passed here model._products[i] , your field names will be prefixed with _products[0] . 由于表达式是在此处传递的model._products[i] ,因此您的字段名称将以_products[0]为前缀。

If your goal with the list template was simply to reuse this portion of the view, you can still do that but the model you pass into should be your EAF class, and then you'd use Html.Partial instead of Html.EditorFor : 如果您使用列表模板的目的仅仅是重用视图的这一部分,您仍然可以这样做,但是传入的模型应该是EAF类,然后使用Html.Partial而不是Html.EditorFor

<%: Html.Partial("ProductListTemplate") %>

ProductListTemplate.cshtml ProductListTemplate.cshtml

@model Namespace.To.EAF

...

<%: Html.EditorFor(model => model._products[i], "ProductCheckBox") %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM