MetadataType and client validation in ASP.NET MVC 2

前端 未结 3 1907
刺人心
刺人心 2021-01-13 02:31

Inherited properties and MetadataType does not seem to work with client side validation in ASP.NET MVC 2.

The validation of our MetadataTypes work a

3条回答
  •  情书的邮戳
    2021-01-13 03:12

    Are you sure? I've got a ASP.NET MVC 2 site set up as you describe and I have client side validation of both required and regex based attributes that works fine. It doesn't work with my own validators (that derive from ValidationAttribute) at the moment though:

    [MetadataType(typeof(AlbumMetadata))]
    public partial class Album {}
    
    public class AlbumMetadata {
        [Required(ErrorMessage = "You must supply a caption that is at least 3 characters long.")]
        [MinLength(3, ErrorMessage = "The caption must be at least {0} characters long.")]
        [RegularExpression(@".{3,}")]
        public string Caption { get; set; }
    }
    

    (MinLength basically provides a more obvious way to see what's happening in the Regular Expression attribute, which was added for testing)

    I then have the following in my view:

    
    
    
    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
    <% Html.EnableClientValidation(); %>
    <% using (Html.BeginForm()) {%>
    
    Album details
    <%= Html.TextBox("Caption", Model.Caption, new { @class = "textbox" })%> <%= Html.ValidationMessage("Caption", "*") %>
     
    <%= Html.CheckBox("IsPublic", Model.IsPublic) %>
    <% } %>

    Which results in the following being output to the client below the form tags (formatted for clarity):

    
    

提交回复
热议问题