So I linked my MVC application to an existing database and I've been trying to add the additional registration fields based on my existing database AspNetUsers table.
I need for the registration page to include a dropdown list containing Company names. I've added this field to the RegisterViewModel and added the SelectList to the AccountController and Register view but application keeps breaking.
Any help regarding how to achieve this will be appreciated.
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; }
}
The View:
<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>