My register page requires a dropdownlist that is populated with names from the Customer table. I have included the Customer foreign key in my AspNetUsers table in order to then reference the Customernames, I've added this field in the registerViewModel and IdentityModel. However not quite sure how to access CustomerNames using the CustomerId in the register form as a dropdownlist. Below is what I've tried:
AccountController:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, CustomerID=model.CustomerID};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CustomerCompanyName");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
View:
<div class="col-md-3">
@Html.Label("Customer ID")
</div>
<div class="col-md-9">
@Html.DropDownListFor(m => m.CustomerID, new SelectList(ViewBag.CustomerID, "CustomerID", "CustomerName"))
</div>