asp.net mvc特性约束验证,常规验证和ajax请求json返回验证 System.ComponentModel和System.ComponentModel.DataAnnotations
发布日期:2021-06-29 17:04:48 浏览次数:2 分类:技术文章

本文共 5287 字,大约阅读时间需要 17 分钟。

控制器

using Mvc_ceshi.ServiceModels;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Newtonsoft.Json;using System.Text;namespace Mvc_ceshi.Controllers{    public class ZhuCeController : Controller    {        // GET: ZhuCe        public ActionResult Index()        {            return View();        }        //注册页面        public ActionResult Register()        {            return View();        }        //错误处理        public ActionResult UserRegister(User_ServiceModels user_Serviceodels)        {            //验证通过            if (ModelState.IsValid)            {                return View("Index");            }            else            {                return View("Register");            }        }        ///         /// ajax请求,json返回        ///         ///         /// 
public String UserRegister_json(User_ServiceModels user_Serviceodels) { //验证不通过 if (!ModelState.IsValid) { StringBuilder msg = new StringBuilder(); msg.Append("{"); msg.Append("\"status\":\""+false+"\","); msg.Append("\"error\":[{"); string error = string.Empty; int i = 0; foreach (var key in ModelState.Keys) { var state = ModelState[key]; if (state.Errors.Any()) { error = state.Errors.First().ErrorMessage; msg.Append("\""+key+"\"" + ":" + "\""+error+"\""); //是否加逗号 if (i < ModelState.Keys.Count - 1) { msg.Append(","); } //break; } i++; } msg.Append("}]"); msg.Append("}"); return msg.ToString(); } //验证通过 else { StringBuilder msg = new StringBuilder(); msg.Append("{"); msg.Append("\"status\":\""+true+"\""); msg.Append("}"); return msg.ToString(); } } }}

业务模型类

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel;using System.ComponentModel.DataAnnotations;namespace Mvc_ceshi.ServiceModels{    ///     /// 业务模型类    ///     public class User_ServiceModels    {        ///         /// ID        ///         public int UserId { get; set; }        ///         /// 账户        ///         [DisplayName("账号")] //显示名        [Required(ErrorMessage ="{0}不能为空")]//必填项        [StringLength(12,MinimumLength =6,ErrorMessage ="{0}长度只能为{2}至{1}")]//字符串长度验证;{0}:显示名,{1}:最大长度,{2}最小长度        public string Account { get; set; }        ///         /// 密码        ///         [DisplayName("密码")]         [Required(ErrorMessage ="{0}不能为空")]        [Compare("Password2",ErrorMessage ="{0}两次密码不一致")]        public string Password1 { get; set; }        ///         /// 密码2        ///         public string Password2 { get; set; }        ///         /// 年龄        ///         [DisplayName("年龄")]         [Required(ErrorMessage ="{0}不能为空")]        [Range(16,50,ErrorMessage ="{0}只能在{1}和{2}之间")]//数字范围(支持小数点)        public int Age { get; set; }        ///         /// 邮箱        ///         [DisplayName("邮箱")]         [Required(ErrorMessage ="{0}不能为空")]        [RegularExpression(@"\w+@\w+.\w+",ErrorMessage ="{0}格式不正确")]//正则表达式验证        public string Email { get; set; }    }}

静态页面表单,请求JSON返回

    
用户注册
账户
密码
确认密码
年龄
邮箱

结果返回:

{"status":"False","error":[{"Account":"账号不能为空","Password1":"密码不能为空","Age":"年龄不能为空","Email":"邮箱不能为空"}]}

静态页面表单,直接请求,返回

    
用户注册
账户 @Html.ValidationMessage("Account")
密码
确认密码 @Html.ValidationMessage("Password1")
年龄 @Html.ValidationMessage("Age")
邮箱 @Html.ValidationMessage("Email")

返回结果展示

转载地址:https://cplvfx.blog.csdn.net/article/details/101017892 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:asp.net mvc 控制器和视图的花式玩法
下一篇:asp.net mvc特性约束后,ModelState 打印输出错误信息

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月24日 02时29分37秒