开发者

MVC3 submit returning null on my complex data type

开发者 https://www.devze.com 2023-03-16 11:18 出处:网络
In my MVC3 project I have the following model: public class CustomerModules { public int ModuleId { get; set; }

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

}

And my view snippet is:

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

开发者_运维技巧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?


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

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

In CustomerModuleList.cshtml put

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

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)

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


This is a similar but unrelated problem. I'm adding it here in hopes that I will save some other poor developer 4+hours of troubleshooting. I don't know if this was just on my system but...in MVC 3 I found that if the Model contains any kind of list property (list, ienumerable, array...) with the name Member or OrgMember the model binder will completely fail! Go figure.


Please check this post : http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/ and I hope it helps you fix the issues in your app :)

PS: The example works on MVC3 too without any additional effort.

0

精彩评论

暂无评论...
验证码 换一张
取 消