2
Answers

dropdownlist validation

ceci bela

ceci bela

10y
865
1
On the create.cshtml file I need to have a default value selected. I got the default value to work but now it won't validate. I googled and someone said that I must use DropDownListFor instead of DropDownList. But I can't get the DropDownListFor to work. Any ideas? Thanks
 
--controller--
 var SelectedState = "New York";
var stateList = new SelectList(db.states, "Name", "Name", SelectedState);
ViewBag.StateOptions = stateList;
 
 --Create.cshtml --
 @Html.LabelFor(model => model.State)
@Html.DropDownList("StateOptions", String.Empty) <=== This works but won't validate.
//@@Html.DropDownListFor(model => model.State,new SelectList(ViewBag.StateOptions,"Id","Name"), "Pick State") <== Tried this too but no luck. @
@Html.ValidationMessageFor(model => model.State)

Output 
 
Answers (2)