ASP.NET MVC 5 RegisterView - how to implement a Dropdown list

152 Views Asked by At

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>
2

There are 2 best solutions below

0
Wowo Ot On

in your Controller

List<SelectListItem> li = new List<SelectListItem>();
// add you data to the list
var query = from of in your_context.Office
            select new SelectListItem()
            {
                  Text = of.Name,
                  Value = of.Id
            };
li = query.ToList();
ViewBag.Office = li;

in the View

<div class="col-md-10">
   @Html.DropDownList("Office", ViewBag.Office as List<SelectListItem>, new { @class = "form-control" })
</div>
0
Sh.Imran On

You need to populate the data in SelectList type field which should be declare in ViewModel class. This field of SelectList type collection would be use in View to bind with Dropdown list.

It is explained in my answer of another post: MVC C# Dropdown list Showing System.Web.SelectListItem on the model and can not blind to controller