Search code examples
asp.net-mvcdatabaseentity-frameworkasp.net-identitydropdown

binding dropdown in asp.net MVC registration


I am desperately looking for help with customizing the registration page of my project. Based on available documentation, I've updated the identity model, the account view model and all works fine apart from the fact that my dropdownlist for gender or country code is manually hard coded. I can't figure out how to populate the dropdownlists from a database using the entity framework. I hence would highly appreciate if somebody could give me a quick walk through of how this is being achieved. I guess it should be fairly simple, but really been trying for days now.

In order to keep maintenance options simple, I want to go with a table on SQL that feeds into the dropdown. Let's take 'Gender' as example. I have hence gone ahead and updated the AccountViewModels and added the following at the bottom:

public class GenderList
{
    public int ID { get; set; }
    public string Gender { get; set; }
}

After that, I went over to the IdentityModels and added the 'public DbSet line, so it looks as follows:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<GenderList> GenderLists { get; set; }
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

And that's where I think I get lost... it's obvious that I have to update the AccountController as well (which I have done...) but I'm not quite sure how to best approach it, as the existing Register.cshtml would not allow me to load multiple models, etc.

For what concerns the controller, I've tried (and failed with...) the following:

public ActionResult GetGender()
    {
        GenderList db = new GenderList();
        ViewBag.Gender = new SelectList(db.Gender, "ID", "Gender");
        return View();
    }

And last but not least, the relevant section of my Register.cshtml:

@Html.LabelFor(m => m.Gender, new { @id = "registerGenderLabel", @class = "registerLabel col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-3 col-lg-offset-1 col-lg-4" })
            @*@{  var gender = new List<SelectListItem>
                {
                    new SelectListItem{ Text = "female", Value = "female" },
                    new SelectListItem { Text = "male", Value = "male"}
                };
            }*@
            @Html.DropDownListFor("Gender","Select")

Solution

  • I'm not sure I entirely understand what you are asking. But for select lists, just add a SelectListItem as a Property of the Model that is the main model for the View.

    In model.

    public <List>SelectListItems AddressPurposes {get; set;}
    public string AddressPurpose {get; set;}
    

    Populate this in the controller as use the DropDownListFor as such, replacing the strings with the values from your db in a loop. Here I'm just adding a hard coded value but you can obviously substitute whatever:

    model.AddressPurposes = new List<SelectListItem>() { };
                        model.AddressPurposes.Add(new SelectListItem() { Text = "New Address", Value = "New Address" });
    

    In view:

    @Html.DropDownListFor(m => m.AddressPurpose, Model.AddressPurposes, new { @class = "input-sm" })