這篇文章主要介紹MVC3如何自定義注解驗證字符長度,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
自定義注解(驗證字符長度)
需要繼承ValidationAttribute類,它是一個抽象類。
需要引用命名空間:
using System.ComponentModel.DataAnnotations;
----------新建一個類(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}字符過長"):向基類的構(gòu)造函數(shù)添加一個默認的錯誤提示信息 public MaxWordsAttribute(int maxWords):base("{0}字符過長") { _maxwords = maxWords; } /// <summary> /// /// </summary> /// <param name="value">要驗證對象的值</param> /// <param name="validationContext">描述執(zhí)行驗證檢查的上下文</param> /// <returns></returns> protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { if (value.ToString().Length > _maxwords) { //validationContext.DisplayName:字段的名稱 //FormatErrorMessage:錯誤消息 var msg = FormatErrorMessage(validationContext.DisplayName); return new ValidationResult(ErrorMessage); } } return ValidationResult.Success; } } }
----------給模型添加數(shù)據(jù)注解
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(10,ErrorMessage="字符過長")] public virtual string st_name { get; set; } public virtual List<School> Schools { get; set; } } }
=============================================子驗證模型
是要繼承接口: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] //不許為空 [Display(Name = "名稱")] public virtual string st_name { get; set; } public virtual List<School> Schools { get; set; } #region IValidatableObject 成員 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if (st_nameConfirm.Length > 3) { yield return new ValidationResult("字符過長", new[] {"st_name" }); } } #endregion } }
以上是“MVC3如何自定義注解驗證字符長度”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道!
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
網(wǎng)站欄目:MVC3如何自定義注解驗證字符長度-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://aaarwkj.com/article34/cdphpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計公司、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容