I have a dropdownlist which is stored within TitleEditorTemplate:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%=Html.DropDownList("",
new SelectList(
new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}
)
)%>
View
<%=Html.EditorFor(x => x.Title, "TitleEditorTemplate")%>
ViewModel
[UIHint("TitleEditorTemplate")]
[Required(ErrorMessage = "Please select a title")]
public string Title { get; set; }
Controller
//Results of a query of textboxes
Title = data.Title,
Problem
My problem occurs when I'm trying to edit (pulling data from a database) the information and then write an UPDATE to the database, everything else is pulled back correctly and inserted into the correct textboxes.
However the dropdownlist automatically selects the first option 'MR' rather than 'MRS'.
I know it must have something to do with how it generates the dropdownlistbut I don't know how to fix this.
DropDownList Code
<select id="Title_TitleDropDown" name="Title.TitleDropDown"><option>MR</option>
<option>MRS</option>
<option>MISS</option>
<option>MS</option>
<option>SIR</option>
<option>REV</option>
<option>DR</option>
</select>
What am I missing? How do I get it to choose the option selected from the database as the default selection?
For example:
Database columns
- Title: DR
- Forename: John
- Surname: Smith
When the user wants to update this information, they can do so. Within my aspx page it will popluate both the forename/surname textboxes however not the dropdownlist, this instead becomes the default value of "MR" so the name becomes Mr John Smith rather than Dr John Smith.