Get the App
SLTechnology News&Howtos  ›  Development  › 

How to customize the character length of annotation validation by MVC3

Shulou Source: shulou.com Published: 2022-06-02 11:45:20 09月10日 Update

This article mainly introduces MVC3 how to customize the character length of annotation verification, which is very detailed and has a certain reference value. Friends who are interested must read it!

Custom comments (verify character length)

You need to inherit the ValidationAttribute class, which is an abstract class.

The namespace needs to be referenced:

Using System.ComponentModel.DataAnnotations

-create a new class (MaxWordsAttribute.cs)

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;namespace SchoolManageDomw.Models {public class MaxWordsAttribute:ValidationAttribute {private readonly int _ maxwords / / base ("{0} character is too long"): add a default error message public MaxWordsAttribute (int maxWords): base ("{0} character too long") {_ maxwords = maxWords to the constructor of the base class } / the value of the object to be validated / describes the context in which the validation check is performed / protected override ValidationResult IsValid (object value ValidationContext validationContext) {if (value! = null) {if (value.ToString (). Length > _ maxwords) {/ / validationContext.DisplayName: name of the field / / FormatErrorMessage: error message var msg = FormatErrorMessage (validationContext.DisplayName) Return new ValidationResult (ErrorMessage);}} return ValidationResult.Success;}

-add data annotations to the model

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Entity;using System.ComponentModel;using System.ComponentModel.DataAnnotations;using System.Web.Mvc;namespace SchoolManageDomw.Models {public class SchoolType:IValidatableObject {[Key] public virtual int st_id {get; set;} [MaxWords (10MagneErrorMessage = "character too long")] public virtual string st_name {get; set } public virtual List Schools {get; set;}

= sub-verification model

Is to inherit the interface: IValidatableObject

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Entity;using System.ComponentModel;using System.ComponentModel.DataAnnotations;using System.Web.Mvc;namespace SchoolManageDomw.Models {public class SchoolType:IValidatableObject {[Key] public virtual int st_id {get; set;} [Required] / / cannot be empty [Display (Name = "name")] public virtual string st_name {get; set } public virtual List Schools {get; set;} # region IValidatableObject member public IEnumerable Validate (ValidationContext validationContext) {if (st_nameConfirm.Length > 3) {yield return new ValidationResult ("character too long", new [] {"st_name"}) }} # endregion}} above is all the content of the article "how to customize the character length of annotation verification in MVC3". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Characters validation comments length content name model article error up and down context value information interest function field object boy partner member Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL OPPO Reno Redmi Shulou Information