MVC4 - how to vaildate a drop down list?
- by Grant Roy
I have a .Net MVC4 model / view with a number of [Required] fields, one of which is selected via a drop down list, "Content_CreatedBy" [the first code block below].
Client side validation fires on all fields except the DDL [although server side validation does not allow no entry in DDL]. I have tried validating on the DDL text as well its numeric value but niether fire on the client side.
Can anyone see what I am doing wrong?
Thanks
Model
[Required]
[Display(Name = "Author")]
[ForeignKey("ContentContrib")]
[Range(1, 99, ErrorMessage = "Author field is required.")]
public virtual int Content_CreatedBy { get; set; }
[Required]
[Display(Name = "Date")]
public virtual DateTime Content_CreatedDate { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Source / Notes ")]
[StringLength(10, MinimumLength = 3)]
public virtual string Content_Sources { get; set; }
[Required]
[Display(Name = "Keywords")]
[StringLength(50, MinimumLength = 3)]
public virtual string Content_KeyWords { get; set; }
VIEW
<div class="editor-label">
@Html.LabelFor(model => model.Content_CreatedBy, new { @class="whitelabel"})
</div>
<div class="editor-field">
@Html.DropDownList("Content_CreatedBy", String.Empty)
@Html.EditorFor(model => model.Content_CreatedBy)
@Html.ValidationMessageFor(model => model.Content_CreatedBy)
</div>