9

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.

8 Answers 8

19

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.

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

2 Comments

I thought the point of it all was to have your fields named after your properties. But be that as it may, I will definitely put that into my knowledge base.
That's was the solution to my problem ... thank you sir !!
7

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) %>

1 Comment

I'm having an issue with my form getting the selected value from the DDL. Mine is setup almost exactly like this except it didn't have the cast to (SelectList) in front of the Model.List. I added that but I get an error that it is unable to cast 'System.Collections.Generic.List[System.Web.Mvc.SelectListItem] to type 'System.Web.Mvc.SelectList'.
5

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.

Comments

3

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>) %>

9 Comments

The only thing that does is display the first option, which is not the right one at any rate.
@JuannStrauss i've modified my answer a bit. this should work now.
It's not. I posted this question because it should, but it doesn't.
I saw that. I don't want to work around the problem though. My concern is WHY something that should work, doesn't. I'll accept your answer, because it is a good one, even though something weird is happening in my app.
The problem is using ViewBag... the dropdownlist looks for something with the same name in the ViewBag. You can change the name of the object holding the list in the ViewBag ( for instance "ViewBag.YearsCyclingDDL"). This will not do any conflicts.
|
2

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....

1 Comment

and how do i put class with this method? [I have tried :]
1

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.

Comments

1

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.

Comments

1

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.

Comments

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.