ceci bela

ceci bela

  • NA
  • 58
  • 12.7k

dropdownlist validation

Nov 3 2014 9:29 AM
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)