asp.net-mvc-5 - 值不能为空参数名称项 MVC 5 (DropDownList)
问题描述
我正在尝试将角色添加到我现有的身份框架中。对于那部分,我修改了我的帐户控制器,如下所示:
public ActionResult Register()
{
List<SelectListItem> list = new List<SelectListItem>();
foreach (var role in RoleManager.Roles)
list.Add(new SelectListItem() { Value = role.Name, Text = role.Name });
ViewBag.Roles = list;
return View();
}
这是我的 Post Register 方法。我已经用 User roles 重新填充了它。
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
result = await UserManager.AddToRoleAsync(user.Id, model.UserRoles);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
//Assign Role to user Here
//await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles.ToString());
//Ends Here
return RedirectToAction("Index", "Users");
}
List<SelectListItem> list = new List<SelectListItem>();
foreach (var role in RoleManager.Roles)
list.Add(new SelectListItem() { Value = role.Name, Text = role.Name });
ViewBag.Roles = list;
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
// If we got this far, something failed, redisplay form
return View(model);
}
并将视图模型注册为
public class RegisterViewModel
{
public int Id { get; set; }
[Display(Name = "User Role")]
public string UserRoles { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
和 Register.cshtml 作为
<div class="form-group">
@Html.LabelFor(m => m.UserRoles, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.UserRoles, new SelectList(ViewBag.Roles, "Value", "Text"),"Select a Role", new {@class = "form-control" })
</div>
</div>
当我点击注册按钮时。抛出一个异常,说值不能为空,参数名称项。
RoleViewModel 包含以下几行
public class RoleViewModel
{
public RoleViewModel() { }
public RoleViewModel(ApplicationRole role)
{
Id = role.Id;
Name = role.Name;
}
public string Id { get; set; }
public string Name { get; set; }
}
解决方案
推荐阅读
- django - How to get a combined django query while preserving order
- bash - 如何遍历bash中给定目录中指定类型的所有文件?
- c++ - Max in matrix with array
- amazon-web-services - Boto3 returns An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
- google-api - Can Google Calendar API support 'service account authorization without OAuth` method?
- node.js - 加入两个集合并在 MongoDb 中应用条件
- java - 使用 Junit 5 参数化对象列表
- javascript - 排序中的 React JS 函数返回 Null
- php - 解决 Laravel 安装 SSL 证书错误
- reactjs - 当用户点击添加新行时如何显示最后一页,以便用户可以使用react js和prime react查看新添加的行