简体   繁体   English

无法在POST上绑定MVC模型

[英]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. 这是页面的ViewModel。

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

And here's the sub-ViewModel for each of the DeleteableManagers: 这是每个DeleteableManagers的子ViewModel:

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: 这是我为DeleteableManagerViewModel创建的EditorTemplate:

@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! 但是当我将表单提交给控制器时,模型会返回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.... 我试着跟着这篇文章: CheckBoxList多个选择:模型中的难度绑定但我必须遗漏一些东西....

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 . 您的模型是一个AddListManagersViewModel ,它AddListManagersViewModel CurrentManagers So, you're posting an array of DeleteableManagerViewModel , which isn't getting bound to the "wrapper" model. 所以,你发布了一个DeleteableManagerViewModel数组,它没有被绑定到“包装器”模型。 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... 我不会使用EditorFor扩展,所以我只是在猜...

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

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