2

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.

2
  • is it always "MR"? Nomather which option you select? Commented Jan 26, 2011 at 15:44
  • Yes, I think it must automatically select the first value. However if I pass this onto the next aspx page and then use my custom back button it remembers what has been selected. Commented Jan 26, 2011 at 15:50

2 Answers 2

3

SelectList takes a selected value as well as available options.

So this example should select MRS.

new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, "MRS")

UPDATE

Here is an example using RenderPartial...

<% Html.RenderPartial("ViewUserControl1", new DropDownModel {Name = "Title", SelectedValue = "MISS"});%>

You can use Model.Title instead of "MISS"...

Here is the DropDownModel

public class DropDownModel
{
    public string Name { get; set; }

    public object SelectedValue { get; set; }
}

Here is the partial view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.DropDownModel>" %>
<%= Html.DropDownList("test", new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}, Model.SelectedValue)) %>

This is just a quick mock-up that shows how you could do it - you might want to improve upon the concept!

Sign up to request clarification or add additional context in comments.

4 Comments

Rather than , "MRS"). Do I have to pass through a Model?
That would be the ideal option - I'll be honest and say I've never done that using a template. I have used RenderPartial, with a partial view that has a real simple model that contains the "object SelectedValue"
It now selected the correct value using the 'SelectedValue', however the user hits the submit button the ViewModel has a blank value in both the Name/SelecteValue. How do I pull this information back out?
Ah, where I have put "test", you will need to use the appropriate name to match your model. For example "Customer.Title" or "Title" for example.
2

I believe the problem is that the property name and the control name don't match. I suspect, though I haven't tested, that the select control is ultimately named Title.TitleDropDown. This prevents the default mapper from assigning the value.

Changing your drop down control to:

<%=Html.DropDownList("",new SelectList(new[] { "MR", "MRS", "MISS", "MS", "SIR", "REV", "DR"}))%>

should fix the problem.

1 Comment

I removed the "TitleDropDown" and still receive the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.