所以我将我的MVC应用程序链接到现有数据库,并且我一直在尝试根据我现有的数据库AspNetUsers表添加其他注册字段。
我需要在注册页面中包含一个包含公司名称的下拉列表。我已将此字段添加到RegisterViewModel并将SelectList添加到AccountController和Register视图,但应用程序仍在中断。
任何有关如何实现这一目标的帮助将不胜感激。
的AccountController:
PosworxDBEntities db = new PosworxDBEntities();
PosworxWebEntities db_ = new PosworxWebEntities();
[AllowAnonymous]
public ActionResult Register()
{
ViewBag.CustomerID = new SelectList(db_.Customers, "CustomerID", "CustomerName");
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
AspNetUser users = new AspNetUser();
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
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>");
ViewBag.CustomerID = new SelectList(db_.Customers, "CustomerID", "CustomerCompanyName", users.Id);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
AccountViewModel:RegisterViewModel
public class RegisterViewModel
{
[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; }
public string CustomerID { get; set; }
}
观点:
<div class="form-group">
<div class="col-md-3">
@Html.LabelFor(m => m.CustomerID)
</div>
<div class="col-md-9">
@Html.DropDownList("CustomerID", null, htmlAttributes: new { @class = "form-control" })
</div>
</div>