views:

106

answers:

3

I have a list of options (IEnumerable< SelectListItem >) in my model that I want to use in multiple dropdowns in my view. But each of these dropdowns could have a different selected option.

Is there an easy way to simply specfiy which should be selected if using the Html.DropDownList helper?

At this point, the only way I can see is to generate the html myself and loop through the list of options like so:

<% for(int i=0; i<10; i++) { %>
    <select name="myDropDown<%= i %>">
        <% foreach(var option in Model.Options) { %>
        <option value="<%= Html.Encode(option.optValue) %>" <%if(ShouldBeSelected(i)) {%> selected="selected"<% } %>><%= Html.Encode(option.optText) %></option>
        <% } %>
    </select>
<% } %>
+2  A: 

Html.DropDownList() accepts a SelectList as a parameter which has a SelectedValue property. Specify the selected item when you create the SelectList and pass the SelectList to the Html.DropDownList().

Nathan Taylor
A: 

Here's an example that has 7 drop downs on the page, each with the same 5 options. Each drop down can have a different option selected.

In my view, I have the following code inside my form:

<%= Html.DropDownListFor(m => m.ValueForList1, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList2, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList3, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList4, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList5, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList6, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList7, Model.AllItems)%>

Then I have a viewmodel like this:

public class HomePageViewModel
{
    public List<SelectListItem> AllItems { get; set; }

    public string ValueForList1 { get; set; }
    public string ValueForList2 { get; set; }
    public string ValueForList3 { get; set; }
    public string ValueForList4 { get; set; }
    public string ValueForList5 { get; set; }
    public string ValueForList6 { get; set; }
    public string ValueForList7 { get; set; }

    public HomePageViewModel()
    {
        AllItems = new List<SelectListItem> 
        {
            new SelectListItem {Text = "First", Value = "First"},
            new SelectListItem {Text = "Second", Value = "Second"},
            new SelectListItem {Text = "Third", Value = "Third"},
            new SelectListItem {Text = "Fourth", Value = "Fourth"},
            new SelectListItem {Text = "Fifth", Value = "Fifth"},
        };
    }
}

Now in your controller method, declared like this:

        public ActionResult Submit(HomePageViewModel viewModel)

The value for viewModel.ValueForList1 will be set to the selected value.

Of course, I'd suggest using some kind of enum or ids from a database as your value.

Mac
+1  A: 

class 'Item' with properties 'Id' and 'Name'
ViewModel class has a Property 'SelectedItemId'
items = IEnumerable<Item>

<%=Html.DropDownList("selectname", items.Select(i => new SelectListItem{ Text = i.Name, Value = i.Id.ToString(), Selected = Model.SelectedItemId.HasValue ? i.Id == Model.SelectedItemId.Value : false })) %>
Mika Kolari