Captain Kenpachi
Captain Kenpachi

Reputation: 7215

ASP.NET MVC dropdownlist not selecting value on render

I have this annoying problem where my DropDownlist doesn't select the current value by default.

Controller:

var YearsCycling = new SelectList(new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
},
"Value",
"Text",
new SelectListItem() { Value = "5-10yrs", Text = "5-10yrs", 
Selected = true });
ViewBag.YearsCycling = YearsCycling;

View:

<%:Html.DropDownListFor(model=>model.YearsCycling,(SelectList)ViewBag.YearsCycling,"-select-") %>

but instead of selecting "5-10yrs", it just shows the "-select-" option and if I inspect the DOM source, none of the elements are selected.

UPDATE: I still don't know what the problem is, but I got it working for now by doing something ugly:

<%
    var sl = (SelectList)ViewBag.YearsCycling;
%>
<select name="YearsCycling" id="YearsCycling">
<%foreach(var li in sl){  %>
    <option value="<%:li.Value %>" <%if(li.Selected){%>selected="true"<%}%>><%:li.Text %></option>
<%} %>
</select>

This isn't the best solution, but if you've come to this question because you've been pulling your hair out, this should help.

Upvotes: 9

Views: 25281

Answers (8)

Imaginary
Imaginary

Reputation: 259

Also Html.DropDownListFor uses values from POST request.

Therefore if you have the same parameter in POST data MVC will use it even if you have different value in model.

Check if you have the same name in request and change it.

Upvotes: 1

Lee Cordell
Lee Cordell

Reputation: 276

Just ensure that the variable name set on ViewBag is different to the name assigned to the dropdownlist - i.e. don't do:

Controller:

ViewBag.CityId = new SelectList(....)

View:

@Html.DropDownList("CityId", ...

Just use a different viewbag variable name.

I've had problems when using the same ViewBag variable name as a html control name with drop down lists and listboxes.

Upvotes: 1

user1630889
user1630889

Reputation:

If you are rendering your list like this: @Html.DropDownListFor(model => model.EntityID, Model.EntitySelectList)

The value .NET MVC will use when rendering the select options will match the value of model.EntityID.ToString().

... It will not select the Model.EntitySelectList.SelectedValue

... Or any of the EntitySelectList.Item.SelectedValue items.

... In other words, when rendering a DropDownListFor something other than the list itself, the SelectedValue properties of both the List and the List Items are ignored.

No matter what Model.EntitySelectList.SelectedValue or Model.EntitySelectList.Item.SelectedValue is set, ASP.NET MVC will not render selection. Use the model.EntityID instead.

Upvotes: 5

NicoJuicy
NicoJuicy

Reputation: 3528

I have some problems when using a ViewBag with the same name as the html-element.

So, doesn't use a DropDownList for EventId, when the SelectList is stored in a ViewBag.EventId.

Change ViewBag.EventId to ViewBag.EventData, and it's fixed.

Upvotes: 1

Brian
Brian

Reputation: 325

I'm sure you're way past this, but I just got burned on this a moment ago because I had named the dropdown list the same name as the property in my view model class.

Changing the dropdown list name to something else cured it right up.

Upvotes: 19

dnxit
dnxit

Reputation: 7350

Binding dropdownlist is very tricky in MVC it embarrassed me a lot you can do it with this in your controller get all your cities list put it in viewBag

Create

      ViewBag.CityId = new SelectList(db.Cities, "ID", "Name");

user.CityID if you are in Edit so that on edit it select the city

      ViewBag.CityId = new SelectList(db.Cities, "ID", "Name", user.CityID);

in your View just do this trick

     @Html.DropDownList("CityId", "Select")

this is the most simplest way I know....

Upvotes: 2

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16134

Try this:

Create a viewmodel:

public class MyViewModel
{
    public string SelectedValue { get; set; }
    public IEnumerable<SelectListItem> YearsCycling { get; set; }
}

Controller:

var YearsCycling = new SelectList(new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs"}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
},
"Value",
"Text");

MyViewModel model = new MyViewModel();
model.YearsCycling = YearsCycling;
model.SelectedValue = "5-10yrs";
return View(model);

View:

<%:Html.DropDownListFor(model=>model.SelectedValue,(SelectList)Model.YearsCycling) %>

Upvotes: 7

scartag
scartag

Reputation: 17680

Your code should be.

var YearsCycling = new List<SelectListItem>() 
{ 
    new SelectListItem(){ Value="1yr", Text="1yr"},
    new SelectListItem(){ Value="1-3yrs", Text="1-3yrs"}, 
    new SelectListItem(){ Value="3-5yrs", Text="3-5yrs"}, 
    new SelectListItem(){ Value="5-10yrs", Text="5-10yrs", Selected=true}, 
    new SelectListItem(){ Value="10+yrs", Text="10+yrs"} 
};

ViewBag.YearsCycling = YearsCycling;

You could use this overload instead.

<%:Html.DropDownListFor(model=>model.YearsCycling,ViewBag.YearsCycling as List<SelectListItem>) %>

Upvotes: 3

Related Questions