In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.