简体   繁体   中英

Unable to Bind MVC Model on POST

I'm writing a view that displays a list of managers. The managers have checkboxes next to their name to select them to be removed from the manager list. I am having problems binding the form submission back to my view model. Here's what the page looks like:

在此输入图像描述

Here's the ViewModel for the page.

public class AddListManagersViewModel
{
    public List<DeleteableManagerViewModel> CurrentManagers;
}

And here's the sub-ViewModel for each of the DeleteableManagers:

public class DeleteableManagerViewModel
{
    public string ExtId { get; set; }
    public string DisplayName { get; set; }

    public bool ToBeDeleted { get; set; }
}

This is the code for the main View:

@model MyApp.UI.ViewModels.Admin.AddListManagersViewModel
<div class="row">
    <div class="span7">
        @using (Html.BeginForm("RemoveManagers","Admin"))
        {
            @Html.AntiForgeryToken()
            <fieldset>
                <legend>System Managers</legend>

                <table class="table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Remove</th>
                        </tr>
                    </thead>

                    <tbody>
                        @Html.EditorFor(model => model.CurrentManagers)
                    </tbody>
                </table>
            </fieldset>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">Delete Selected</button>
            </div>
        }
    </div>
</div>

And this is the EditorTemplate I've created for DeleteableManagerViewModel:

@model MyApp.UI.ViewModels.Admin.DeleteableManagerViewModel

<tr>
    <td>@Html.DisplayFor(model => model.DisplayName)</td>
    <td>
        @Html.CheckBoxFor(model => model.ToBeDeleted)
        @Html.HiddenFor(model => model.ExtId)
    </td>
</tr>

But when I submit the form to the controller the model comes back null! this is what I want it to do:

[HttpPost]
public virtual RedirectToRouteResult RemoveManagers(AddListManagersViewModel model)
{
    foreach (var man in model.CurrentManagers)
    {
        if (man.ToBeDeleted)
        {
            db.Delete(man.ExtId);
        }           
    }
    return RedirectToAction("AddListManagers");
}

I tried following along this post: CheckBoxList multiple selections: difficulty in model bind back but I must be missing something....

Thanks for your help!

Hmm. I think this is ultimately the problem; here's what you're posing:

CurrentManagers[0].ToB‌​eDeleted=true&CurrentManagers[0].ToBeDeleted=false&CurrentManagers[0].Ext‌​Id=X00405982144

Your model is an AddListManagersViewModel that has a collection of CurrentManagers . So, you're posting an array of DeleteableManagerViewModel , which isn't getting bound to the "wrapper" model. You can try changing the model parameter to

params DeleteableManagerViewModel[] model

I don't ever use the EditorFor extensions, though, so I'm just guessing...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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