MVC3提交返回我的复杂数据类型空 [英] MVC3 submit returning null on my complex data type

查看:108
本文介绍了MVC3提交返回我的复杂数据类型空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my MVC3 project I have the following model:

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; }
}

我的控制器如下:

My controller is as follows:

[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);

}

和我的看法片段是:

<% 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 ?想法吗?

When I submit back to the controller the IEnumerable<CustomerModules> Modules comes back as a null. I would like to know how you can submit an IEnumerable of a complex type in MVC3? Ideas anyone?

推荐答案

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

The problem is you're not specifying that the object your form is submitting is of type CustomerModuleList.

在你的意见/共享/ EditorTemplates文件中创建一个名为CustomerModuleList.cshtml和CustomerModules.cshtml意见。

In your views/Shared/EditorTemplates file create views named CustomerModuleList.cshtml and CustomerModules.cshtml.

在CustomerModuleList.cshtml把

In CustomerModuleList.cshtml put

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

,然后在CustomerModules.cshtml粘

and then in CustomerModules.cshtml stick

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

然后在您的视图代码片段,其中

then in your view snippet replace the for loop with

Html.EditorFor(x => Model)

用不同的IEnumerable型我做了测试这和它的工作。

tested this with a different IEnumerable of a type I made and it worked.

这篇关于MVC3提交返回我的复杂数据类型空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆