0

Can someone please show me how to get a couple of dynamic select fields into the MVC Register Form that comes with Visual Studio 2013 MVC project?

I have modified the AspNetUsers table to include TitleId and SexId, I have also modified the registration form with just 2 extra fields:

@Html.TextBoxFor(m => m.TitleId, new { @placeholder = "Title *", @type = "number" })
@Html.TextBoxFor(m => m.SexId, new { @placeholder = "Sex *", @type = "number" })

The form works but I need TitleId and SexId to be Select fields with data from the respective Title and Sex tables.

Any help would be much appreciated.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
iggyweb
  • 2,373
  • 12
  • 47
  • 77
  • Your IDs won't be enough, since they only represent the item that needs to be selected. In addition you need the list of selectable items for each group (title and sex). This question might give you a hint: http://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor – Krisztián Balla May 08 '14 at 10:21

1 Answers1

0

On reflection it seems a waste to take trips to the database for these records, they will never change so I have added the following into the Register form:

@Html.DropDownListFor(m => m.TitleId, new List<SelectListItem> { new SelectListItem { Value = "", Text = "Title" }, new SelectListItem { Value = "1", Text = "Mr" }, new SelectListItem { Value = "2", Text = "Mrs" }, new SelectListItem { Value = "3", Text = "Miss" }, new SelectListItem { Value = "4", Text = "Dr" } })

@Html.DropDownListFor(m => m.SexId, new List<SelectListItem> { new SelectListItem { Value = "", Text = "Sex" }, new SelectListItem { Value = "1", Text = "Male" }, new SelectListItem { Value = "2", Text = "Female" }})

As a result the Register form generates and functions correctly.

iggyweb
  • 2,373
  • 12
  • 47
  • 77
  • Another idea would be to read the records from the database and cache them. That way you wouldn't have to modify your code after adding new titles and entries can't run out of sync. – Krisztián Balla May 08 '14 at 11:29