I am validating a form using default MVC validation technique as follows:
<div class="editor-label">
@Html.LabelFor(model => model.Company_Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Company_Name)
@Html.ValidationMessageFor(model => model.Company_Name, "Company Name is required")
</div>
This is working fine for textboxes. When i applied the same for dropdown this is not working.
<div class="editor-label">
@Html.LabelFor(model => model.State_Code, "State")
</div>
<div class="editor-field">
@Html.DropDownList("State_Code", "--Select--")
@Html.ValidationMessageFor(model => model.State_Code,"State is required")
</div>
How to validate for a dropdown in mvc3.Default will be "--Select--"
I think you might need to use @Html.DropDownListFor() in order for the model binding for validation to work, which means the SelectList will have to made by the model.
Typically this is how I set it up: