我对此有点挠头。有时,此代码有效,我无法弄清楚为什么现在出现此错误。该错误显示在Visual Studio的剃须刀页面上。我需要从此下拉列表中获取的项目显示在帖子中。是否出于某种原因又回到了认为需要重新填充列表的观点?当然不会保存任何东西。我的调试显示
之后到了那里await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
它说:这是该线程从当前函数返回时要执行的下一条语句。在dropdownList旁边。控制器具有AddressId。该表格正确填充了下拉菜单。这是在Submit上发生的。
下面是我的控制器,viewModel和probem字符串
public class RegisterUserViewModel
{
public static RegisterUserViewModel GetCompanyId(string userId, CustomerEntities db)
{
var qCust = from ad in db.Addresses
join anu in db.Users on ad.AddressId equals anu.AddressId
join cus in db.CompanyNames on ad.CompanyId equals cus.CompanyId
where (anu.AspNetUsersId == userId)
select new RegisterUserViewModel()
{
UserId = userId,
CompanyId = cus.CompanyId,
AddressId = ad.AddressId
};
var result = qCust.SingleOrDefault();
if (result != null)
{
result.Addresses = db.Addresses.Where(a => a.CompanyId == result.CompanyId);
};
return result;
}
[Required]
[Display(Name = "User Name")]
public string UserName { 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; }
[Required]
[Display(Name = "User Address")]
public int AddressId { get; set; }
[Required]
[Display(Name = "User Roles")]
public string UserRoles { get; set; }
public string UserId { get; set; }
public int CompanyId { get; set; }
public virtual IEnumerable<Addresses> Addresses { get; set; }
}
控制器:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> RegisterUser(RegisterUserViewModel model)
{
if (ModelState.IsValid)
{
var user = new Models.ApplicationUser { UserName = model.UserName, Email = model.Email, AddressId = model.AddressId, CompanyId = model.CompanyId };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var UserId = User.Identity.GetUserId();
//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);
var viewModel = new Users();
{
viewModel.UsersId = Convert.ToString(Guid.NewGuid());
viewModel.Email = model.Email;
viewModel.FirstName = model.UserName;
viewModel.AspNetUsersId = user.Id;
viewModel.CreatedDate = System.DateTime.Now;
viewModel.UpdatedDate = System.DateTime.Now;
};
UsersBusinessModels register = new UsersBusinessModels();
var results = await register.insertUser(viewModel);
return RedirectToAction("AddUsers", "Customer", new { UserId });
}
ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
.ToList(), "Name", "Name");
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
问题字符串:
@Html.DropDownList("AddressId", new SelectList(Model.Addresses, "AddressId", "LocationName"), " Select Location user belongs to ", new {@id = "selection", @class = "form-control" })
我在页面底部也有这个
<script type="text/javascript">
window.onload = function () { document.getElementById("selection").selectedIndex = 0;
</script>
更新:我主要关心的还是我的问题是,当发布具有在数据库中生成记录所需的一切时,为什么为什么要回到视图呢?偶尔我会绊倒一些事情。这是那些时代之一。只是在寻找可能导致该问题的原因,或者我在这段代码中有什么是有效的-差不多一年前,当我编写该代码时,它现在无效了?
更新:添加get操作。 注意:GET没有问题,因为它最初显示的表单没有问题。只有Post出错。
// GET: /Account/RegisterUser
[AllowAnonymous]
public ActionResult RegisterUser(string userId)
{
ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
.ToList(), "Name", "Name");
RegisterUserViewModel modelInstance = RegisterUserViewModel.GetCompanyId(userId, db);
if (modelInstance == null)
{
return RedirectToAction("AddUsers", "Customer");
}
return View(modelInstance);
}