删除下拉列表中的条目导致“值”不能为空.参数名称:item "

本文关键字:参数 quot item 不能 下拉列表 删除 | 更新日期: 2023-09-27 18:16:29

我有两个表单:一个用于为用户分配角色,另一个用于从用户删除角色。视图和控制器非常相似。它们是(表单本身):

AssignRole.cshtml

            @using (Html.BeginForm("AssignRole", "User", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(m => m.UserID)
                <div class="form-group">
                    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @readonly = "readonly" })
                        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.RoleName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.RoleName, new SelectList(Model.UnassignedRoles, "Value", "Text"), Resources.DropdownSelect, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="@Resources.Assign" class="btn btn-default" />
                    </div>
                </div>
            }

RemoveRole.cshtml

            @using (Html.BeginForm("RemoveRole", "User", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(m => m.UserID)
                <div class="form-group">
                    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @readonly = "readonly" })
                        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.RoleName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.RoleName, new SelectList(Model.AssignedRoles, "Value", "Text"), Resources.DropdownSelect, new { @class = "form-control" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="@Resources.Remove" class="btn btn-default" />
                    </div>
                </div>
            }

最后,这里是控制器和它们使用的动作:

UserController.cs

        //
        // GET: /User/AssignRole
        [Authorize(Roles = "Admin")]
        [HttpGet]
        public ActionResult AssignRole(string userID)
        {
            var user = context.Users.Where(u => u.Id == userID).FirstOrDefault();
            var vm = new UserAssignRoleViewModel();
            vm.UserID = user.Id;
            vm.UserName = user.UserName;
            List<IdentityRole> unassignedRoles = new List<IdentityRole>();
            foreach (var role in context.Roles)
            {
                if (this.UserManager.IsInRole(vm.UserID, role.Name) == false)
                {
                    unassignedRoles.Add(role);
                }
            }
            vm.UnassignedRoles = unassignedRoles.OrderBy(r => r.Name).Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            return View(vm);
        }
        //
        // POST: /User/AssignRole
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult AssignRole(UserAssignRoleViewModel vm)
        {
            this.UserManager.AddToRole(vm.UserID, vm.RoleName);
            ViewBag.ResultMessage = Resources.RoleAssignedSuccessfully;
            List<IdentityRole> unassignedRoles = new List<IdentityRole>();
            foreach (var role in context.Roles)
            {
                if (this.UserManager.IsInRole(vm.UserID, role.Name) == false)
                {
                    unassignedRoles.Add(role);
                }
            }
            vm.UnassignedRoles = unassignedRoles.OrderBy(r => r.Name).Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            return View(vm);
        }
        //
        // GET: /User/RemoveRole
        [Authorize(Roles = "Admin")]
        [HttpGet]
        public ActionResult RemoveRole(string userID)
        {
            var user = context.Users.Where(u => u.Id == userID).FirstOrDefault();
            var vm = new UserRemoveRoleViewModel();
            vm.UserID = user.Id;
            vm.UserName = user.UserName;
            vm.AssignedRoles = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            List<IdentityRole> assignedRoles = new List<IdentityRole>();
            foreach (var role in context.Roles)
            {
                if (this.UserManager.IsInRole(vm.UserID, role.Name) == true)
                {
                    assignedRoles.Add(role);
                }
            }
            vm.AssignedRoles = assignedRoles.OrderBy(r => r.Name).Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            return View(vm);
        }
        //
        // POST: /User/RemoveRole
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult RemoveRole(UserRemoveRoleViewModel vm)
        {
            if (this.UserManager.IsInRole(vm.UserID, vm.RoleName))
            {
                this.UserManager.RemoveFromRole(vm.UserID, vm.RoleName);
                ViewBag.ResultMessage = Resources.RoleUnassignedSuccessfully;
                List<IdentityRole> assignedRoles = new List<IdentityRole>();
                foreach (var role in context.Roles)
                {
                    if (this.UserManager.IsInRole(vm.UserID, role.Name) == true)
                    {
                        assignedRoles.Add(role);
                    }
                }
            }
            else
            {
                ViewBag.ResultMessage = Resources.ThisUserDoesNotBelongToSelectedRole;
            }
            return View (vm);
        }

问题是:

下拉菜单每次都要重新填充,要么给用户分配角色,要么删除角色。在分配角色方面一切都很顺利;它只在下拉框中显示未分配的角色,当您添加角色时,从POST操作返回时,它会显示刷新的下拉框,而不包含您刚刚分配的角色。

但是在删除角色方面,一旦您从用户中删除角色(它正确地执行了),从POST操作返回到视图时,它会抛出异常

Value不能为空。参数名称:items

@Html.DropDownListFor(m => m.RoleName, new SelectList(Model.AssignedRoles, "Value", "Text"), Resources.DropdownSelect, new { @class = "form-control" })

我的猜测是,因为在POST Action方法RemoveRole中,我没有以任何方式改变UserRemoveRoleViewModelRoleName属性,并且回到视图下拉框已经重新填充,m => m.RoleName崩溃,因为它正在寻找已经删除的角色,它不再在列表中了。希望我解释得足够清楚。

问题是我完全不知道如何解决这个问题。任何帮助吗?

删除下拉列表中的条目导致“值”不能为空.参数名称:item "

看起来第二个方法,对于移除,缺少了用角色列表重新初始化模型对象的行。因为ASP。. NET MVC是无状态的,这个重新初始化必须在每个请求上完成,即使返回的视图是相同的。

添加这一行:

vm.AssignedRoles = assignedRoles.OrderBy(r => r.Name).Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();

在foreach以RemoveRole结尾之后,错误应该会消失。

问题是您从未将assigndroles集合分配给您的视图属性(assigndroles)。

同样,您可以使用LINQ语句来创建列表,从而使代码更简洁。

在您的GET操作中,您填充视图模型的assigndroles属性。在POST操作中,您无法做到这一点。所以当它到达视图代码时,它是空的,助手不能从一个空集合中创建一个下拉列表。您需要在POST操作中填充它。(请注意,它不会在GET和POST操作之间保持填充状态。)

相关文章: