我有一个未处理的异常,它涉及添加一个下拉列表,如果我不绑定它,则注册页面有一个空的下拉列表。在注册用户时,我需要为该用户选择一个角色。
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation("User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
我的ViewModel是:
[Required]
[Display(Name = "Membership Type")]
public string SelectedMembershipType { get; set; }
public List<MembershipTypeViewModel> MembershipType { get; set; }
Register.cshtml
<div class="form-group">
@Html.LabelFor(m => m.SelectedMembershipType, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(viewModel => viewModel.SelectedMembershipType, new SelectList(Model.MembershipType, "Id", "Name"), " Select Membership ", new { @class = "form-control" })
</div>
</div>