MaxLength Attribute not generating client-side validation attributes

前端 未结 11 547
北荒
北荒 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:22

    Try using the [StringLength] attribute:

    [Required(ErrorMessage = "Name is required.")]
    [StringLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
    public string Name { get; set; }
    

    That's for validation purposes. If you want to set for example the maxlength attribute on the input you could write a custom data annotations metadata provider as shown in this post and customize the default templates.

提交回复
热议问题