asp.net mvc特性约束验证,常规验证和ajax请求json返回验证 System.ComponentModel和System.ComponentModel.DataAnnotations

控制器

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":"邮箱不能为空"}]}

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


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

返回结果展示

asp.net mvc特性约束验证,常规验证和ajax请求json返回验证 System.ComponentModel和System.ComponentModel.DataAnnotations_第1张图片

你可能感兴趣的:(asp.net,mvc)

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