Want to implement a dropdown list on RegisterView cant figure out how to do it I'm new to asp.net mvc5 and following code first approach . I already have a table for Offices. I will list down all my coding. I think ( I'm not sure) i have to initialize the list but dont know how to do it and where to put it
Have a model Office.Cs
public class Office
{
public byte Id { get; set; }
public string Name { get; set; }
}
}
Identity Model
public IEnumerable<Office> Office { get; set; }
[Required]
public byte OfficeId { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
AccountViewModel.cs
public class RegisterViewModel
{
public OfficeViewModel OfficeViewModel { get; set; }
[Required]
[Display(Name = "Office")]
public byte OfficeId { get; set; }
AccountController.cs
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, OfficeId= model.OfficeId};
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>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
RegisterView.cshtml
<div class="form-group">
@Html.LabelFor(m => m.OfficeId)
@Html.DropDownListFor(m => m.OfficeId, new SelectList(Model.Office, "Id", "Name"), "Select", new { @class = "form-control" })
</div>
in your Controller
in the View