0

I have an admin page in my MVC5 web application where a user is selected and upon clicking the proceed button a list of all roles with checkboxes appear (via Ajax). If a user is in any of these roles, the checkbox will be checked automatically.

I want to pass a List in my HttpPost method once the user checks/unchecks the boxes for each role. However, the parameter to my action method is null but there are values in Request.Form.

I don't understand why that is. Also do I really need to use @Html.HiddenFor() for each parameter in my viewmodel in order for things to work correctly?

RoleCheckBoxViewModel

public class RoleCheckBoxViewModel
{

    [Display(Name = "Choose Role(s)")]
    [Key]
    public string RoleId { get; set; }
    public string UserId { get; set; }
    public string Name { get; set; }
    [Display(Name="boxes")]
    public bool IsChecked { get; set; }

}

RolesController Action

[HttpPost]
public ActionResult Update(List<RoleCheckBoxViewModel> list) //the model in my view is a List<RoleCheckBoxViewModel> so then why is it null?
{
      //these next two lines are so that I can get to the AddToRole UserManagerExtension 
      var userStore = new UserStore<ApplicationUser>(_context);
        var userManager = new UserManager<ApplicationUser>(userStore);
        foreach (var item in Request.Form) //it's messy but I can see the data in this Form
        {
            System.Console.WriteLine(item.ToString());
        }
        //AddToRole for any new checkboxes checked
        //RemoveFromRole any new checkboxes unchecked
        //context save changes

        return RedirectToAction("Index");
}

The AllRoles Partial View (A result of a previous AJAX call)

@model List<Core.ViewModels.RoleCheckBoxViewModel>



@using (Html.BeginForm("Update", "Roles", FormMethod.Post))
{
<p>
    Select Roles
</p>
<table class="table">

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => item.Name)
                @Html.CheckBoxFor(model => item.IsChecked)
            </td>
        </tr>
        @Html.HiddenFor(model => item.RoleId)
        @Html.HiddenFor(model => item.UserId)
        @Html.HiddenFor(model => item.IsChecked)
        @Html.HiddenFor(model => item.Name)

    }
</table>
<input type="submit" value="Save" class="glyphicon glyphicon-floppy-save" />
}

  1. find -type f | git check-ignore --stdin gives all files ignored in whole repo (and if you have some in .git that fit, it will report them too).
  2. git ls-files --exclude-standard --ignored --others - gives ignored file per current directory (not whole repository).

Both solutions honor not just top-level .gitignore file, but also global one, .git/info/exclude and local (in lower-level dirs) .gitignores

4

1 回答 1

1

集合情况下的模型绑定器需要对输入控件的名称进行索引,以将其作为集合发布到控制器操作中。您可以将循环更改为使用 for 循环,并在辅助方法中进行索引,是的,如果您不希望用户编辑要发布的属性,则需要为要发布的属性创建隐藏输入。

您可以将循环代码更改为如下所示以正确回发模型:

@for(int i=0; i < Model.Count' i++)
{
    <tr>
        <td>
            @Html.DisplayFor(model => Model[i].Name)
            @Html.CheckBoxFor(model => Model[i].IsChecked)
        </td>
    </tr>
        @Html.HiddenFor(model => Model[i].RoleId)
        @Html.HiddenFor(model => Model[i].UserId)
        @Html.HiddenFor(model => Model[i].IsChecked)
        @Html.HiddenFor(model => Model[i].Name)

}

希望能帮助到你!

于 2018-01-05T20:58:28.623 回答