MaxLength Attribute not generating client-side validation attributes

前端 未结 11 535
北荒
北荒 2020-11-30 02:41

I have a curious problem with ASP.NET MVC3 client-side validation. I have the following class:

public class Instrument : BaseObject
{
    public int Id { get         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 03:20

    I just used a snippet of jquery to solve this problem.

    $("input[data-val-length-max]").each(function (index, element) {
       var length = parseInt($(this).attr("data-val-length-max"));
       $(this).prop("maxlength", length);
    });
    

    The selector finds all of the elements that have a data-val-length-max attribute set. This is the attribute that the StringLength validation attribute will set.

    The each loop loops through these matches and will parse out the value for this attribute and assign it to the mxlength property that should have been set.

    Just add this to you document ready function and you are good to go.

提交回复
热议问题