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"})