开发者

How do I select a value for a dropdownlist from a database in ASP.NET MVC and C#?

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, th开发者_Python百科ey 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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜