Custom validation in MVC not executing on partial views

前端 未结 1 1661
温柔的废话
温柔的废话 2021-01-29 00:14

So I have file uploading input which is strongly typed and below is the model class

public class UploadImageAlbum
{
    [CustomFileValidator]
    pu         


        
相关标签:
1条回答
  • 2021-01-29 01:01

    In order to get client side validation, your attribute must

    1. implement IClientValidatable which will add the associated data-val-* attributes to you html, and
    2. you must include scripts to add methods to the jQuery validator.

    This article is a good guide to creating custom client and server side validation attributes.

    Note also your current attribute is rather limited in that the file types and size are fixed, and it would be more flexible to include properties to specify the file types and maximum file size so that you could use it as (say)

    [FileValidation(MaxSize="1024", FileType="jpg|png")]
    public HttpPostedFileBase Images { get; set; }
    

    This article provide an example of an attribute that validates the file type, but could be adapted to include the MaxSize property.

    Side note: If your loading dynamic content, then you should first set the validator to null

    var form = $('form#frmUploadImages');
    form.data('validator', null);
    $.validator.unobtrusive.parse(form);
    
    0 讨论(0)
提交回复
热议问题