asp.net-mvc-2asp.net-mvc-2-validation

how to validate dropdown optionlist required functionality asp.net mvc


I have dropdown like this ,

<%= Html.OptionList("Worktype", new SelectList(new List<SelectListItem>{ 
new SelectListItem{Text = "--Select One--", Value = "0", Selected=true}, 
new SelectListItem{Text = "Fulltime", Value = "Full Time"}, 
new SelectListItem{Text = "Partime", Value = "Part Time"}}, "Value", "Text" )) %>

After selecting either fulltime or parttime it should submit, but because the default select is there, required validation is passing. I want the required validation for below two options. can anyone help me out.

thank you,

michael



Solution

  • SetValue empty instead of 0 for "--Select One--"

    new SelectListItem{Text = "--Select One--", Value = string.Empty , Selected=true}