MVC4 - how to vaildate a drop down list?
Posted
by
Grant Roy
on Stack Overflow
See other posts from Stack Overflow
or by Grant Roy
Published on 2012-11-13T10:26:21Z
Indexed on
2012/11/13
11:01 UTC
Read the original article
Hit count: 274
asp.net-mvc
|dataannotations
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>
© Stack Overflow or respective owner