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":"邮箱不能为空"}]}
静态页面表单,直接请求,返回
返回结果展示
转载地址:https://cplvfx.blog.csdn.net/article/details/101017892 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月24日 02时29分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
深入剖析Redis RDB持久化机制
2019-04-30
深入Redis内部-Redis 源码讲解
2019-04-30
Redis RDB文件格式全解析
2019-04-30
Redis集群教程
2019-04-30
Redis 集群规范
2019-04-30
Redis事务(transaction)
2019-04-30
Redis通信协议(protocol)
2019-04-30
Redis发布与订阅(pub/sub)
2019-04-30
Redis复制(Replication)
2019-04-30
键空间通知(keyspace notification)
2019-04-30
Hadoop Hive sql语法详解
2019-04-30
如何理解RESTful的幂等性
2019-04-30
Mybatis 中$与#的区别
2019-04-30
深入了解MyBatis返回值
2019-04-30
MyBatis多数据源配置(读写分离)
2019-04-30
MyBatis 执行动态 SQL
2019-04-30
从命令行运行 MyBatis Generator
2019-04-30
MyBatis-Spring配置简单了解
2019-04-30
Mybatis极其(最)简(好)单(用)的一个分页插件
2019-04-30
Mybatis通用Mapper
2019-04-30