2

I am working with a MVC application, and it creates a dropdown list with:

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.TitleList, "Value", "Text", Model.Title), "-- Select Title--", new {@class = "form-control"})

The same code is used for other dropdowns, and they work as expected, but in this case the Model's value for "Title" is not selected. It does show the values, and when it is saved, it saves the correct value. I have also confirmed that the value for "Title" in the model is correct at runtime.

Does anyone know why this isn't auto-selecting the current value?

Thanks.

Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
Sako73
  • 9,957
  • 13
  • 57
  • 75

3 Answers3

3

This raises another question as to what worked in the other approaches where it was used

DropdownList vs DropdownListFor

This two methods are actually confusing in usage, I will give basic examples

using the code below

@Html.DropDownListFor(m => m.SelectedProductId, Model.ProductList, "- Please select -", new {@class="form-control"}) 

Model.ProductList is a SelectList item that looks like SelectList(products, "ID", "Name"), this means the ID is bound to the value while the Name is bound to the text.

for the below code

@Html.DropDownList("SelectedProductId", Model.ProductList, "- Please select -", new {@class="form-control"}) 

Model.ProductList is also a SelectList item that looks like this SelectList(products, "ID", "Name") which is pretty much the same as the first one.

Both of this codes above works when you want to put values in a drop-down select field.

But When you want to pre-select an already selected value the approach changes for the two

For

 @Html.DropDownListFor(m => m.SelectedProductId, Model.ProductList, "- Please select -", new {@class="form-control"}) 

because it is strongly typed with the model binded, it is not neccessary to include the selected value as in SelectList(products, "ID", "Name", "Selected Value"), the binding is supposed to be enough, which is the likely issue you are having

For

@Html.DropDownList("SelectedProductId", Model.ProductList, "- Please select -", new {@class="form-control"}) 

Becaused it is not strongly binded, you need to include the selected value in the SelecteList.

NOTE

Using DropdownList without including the selected value in the Select list works too but the string name has to be the same with the model parameter the text cases doesn't matter eg m => m.Sex and "Sex" or "sex"

So my humble suggestion or opinion is to use

@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.TitleList, "Value", "Text"), "-- Select Title--", new {@class = "form-control"})
Bosco
  • 1,536
  • 2
  • 13
  • 25
1

If you are using DropDownListFor and pass in data which have a value of 0 then it will automatically default to that no matter what you do pretty much.

Two solutions to this is:

  1. Create the HTML select manually in razor then populate it by doing a loop through ViewBag.TitleList. This will give you a bit more control and you will be able to set the default value easier.

<select>
    <option>Select Title</option>
    @foreach (var row in ViewBag.TitleList)
    {
        <option value="@row.Value">@row.Title</option>
    }
</select>
  1. Use javascript when the page is loaded then just select the value "-- Select Title--"

 $('#idOfDropDown[text="-- Select Title--"]').attr("selected","selected");
Tom Dee
  • 2,516
  • 4
  • 17
  • 25
0

I turns out that this question had the answer: Html.DropdownListFor selected value not being set

I have it changed in the code posted above, and it wasn't working, but a clean and rebuild solved the problem. Originally the ViewBag property was named "Title", the same as the model property.

Thanks for the help.

Sako73
  • 9,957
  • 13
  • 57
  • 75