【问题标题】:MVC3 submit returning null on my complex data typeMVC3 提交在我的复杂数据类型上返回 null
【发布时间】:2013-04-20 05:41:25
【问题描述】:

在我的 MVC3 项目中,我有以下模型:

public class CustomerModules
{
    public int ModuleId { get; set; }
    public string ModuleName { get; set; }
    public int CustId { get; set; }
    public bool IsActive { get; set; }
    public DateTime? ActiveDate { get; set; }
}

public class CustomerModuleList
{
    public IEnumerable<CustomerModules> Modules { get; set; }
}

我的控制器如下:

[HttpGet]
public ActionResult EditModules(int custNo)
{
    var model = new CustomerModuleList
    {
        Modules = _customerModules.LoadModulesByCustomerId(custNo)
    };

    return View(model);
}

[HttpPost]
public ActionResult EditModules(CustomerModuleList model)
{
    if (ModelState.IsValid)
    {
       var custId = model.Modules.First().CustId;

       _customerModules.UpdateCustomerModules(model.Modules, custId);
    }

    return View(model);

}

而我的观点 sn-p 是:

<% using (Html.BeginForm("EditModules","Admin",FormMethod.Post, new { enctype = "multipart/form-data" })){ %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>CustomerModuleList</legend>

        <table>
            <tr>
                <th>Module Name</th>
                <th>Active</th>
                <th>Active Date</th>
            </tr>
            <% foreach (var module in Model.Modules){%>
                <tr>

                    <td><%:Html.Label(module.ModuleName) %></td>
                    <td>
                        <%CustomerModules module1 = module;%>
                        <%:Html.CheckBoxFor(x=>module1.IsActive) %>
                    </td>
                    <td><%:Html.Label(module.ActiveDate.ToString()) %></td>
                </tr>
            <% } %>
        </table>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

当我提交回控制器时,IEnumerable&lt;CustomerModules&gt; 模块返回为空。我想知道如何在 MVC3 中提交复杂类型的 IEnumerable?有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    问题是您没有指定表单提交的对象是 CustomerModuleList 类型。

    在您的 views/Shared/EditorTemplates 文件中创建名为 CustomerModuleList.cshtml 和 CustomerModules.cshtml 的视图。

    在 CustomerModuleList.cshtml 中放

    @model CustomerModuleList
    <% foreach (var module in Model.Modules){%>
                Html.EditorFor(x => module);
            <% } %>
    

    然后在 CustomerModules.cshtml 中粘贴

    @model CustomerModules
    <tr>
                    <td><%:Html.Label(Model.ModuleName) %></td>
                    <td>
                        <%:Html.CheckBoxFor(x=>x.IsActive) %>
                    </td>
                    <td><%:Html.Label(Model.ActiveDate.ToString()) %></td>
                </tr>
    

    然后在您看来 sn-p 将 for 循环替换为

    Html.EditorFor(x => Model)
    

    用我制作的另一种类型的 IEnumerable 对此进行了测试,并且成功了。

    【讨论】:

      【解决方案2】:

      这是一个类似但不相关的问题。我在这里添加它是希望我能为其他一些可怜的开发人员节省 4 小时以上的故障排除时间。我不知道这是否只是在我的系统上,但是......在 MVC 3 中,我发现如果模型包含任何类型的列表属性(列表、ienumerable、数组......),名称为 Member 或 OrgMember,模型绑定器将彻底失败!去图吧。

      【讨论】:

      • 非常感谢,我使用 MVC 4 并且遇到了同样的问题。但是我的列表被称为其他名称,我创建了这个问题,但我发现重命名列表名称有帮助...stackoverflow.com/questions/22982096/…
      【解决方案3】:

      请查看此帖子:http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/,希望它可以帮助您解决应用中的问题 :)

      PS:该示例也适用于 MVC3,无需任何额外工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-14
        • 2014-06-27
        • 2013-05-26
        • 1970-01-01
        相关资源
        最近更新 更多