成都创新互联网站制作重庆分公司

MVC3----数据注解与验证(2)之详解Remote验证与Compare验证

***************************************************Remote验证

成都创新互联拥有网站维护技术和项目管理团队,建立的售前、实施和售后服务体系,为客户提供定制化的成都网站设计、网站制作、外贸营销网站建设、网站维护、绵阳主机托管解决方案。为客户网站安全和日常运维提供整体管家式外包优质服务。我们的网站维护服务覆盖集团企业、上市公司、外企网站、购物商城网站建设、政府网站等各类型客户群体,为全球千余家企业提供全方位网站维护、服务器维护解决方案。

概要:

        如果要实现像用户注册那样,不允许出现重复的账户,就可以用到Remote验证。Remote特性允许利用服务器端的回调函数执行客户端的验证逻辑。它只是在文本框中输入字符的时候向服务器提交get请求,Remote验证只支持输入的时候验证,不支持提交的时候验证,这存在一定的安全隐患。所以我们要在提交的时候也要验证,验证失败了,就添加上ModelError

MVC3----数据注解与验证(2)之 详解Remote验证与Compare验证

 

实现:

-------模型代码:

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
    {
        [Key]
        public virtual int st_id { get; set; }
        //       要调用的方法            控制器名称
        [Remote("CheckUserName", "SchoolType")]//Remote验证
        public virtual string st_name{get;set;}
        
        public virtual List Schools { get; set; }
    }
}

 

-------控制器代码:

需要的名称控件:

using System.Web.Security;
using System.Web.UI;

       private SchoolDBContext db = new SchoolDBContext();
        /// 
        /// 定义一个方法,做唯一判断
        /// 
        /// 
        /// 
        private bool IsDistinctStName(string st_name)
        {
            if (db.SchoolTypes.Where(r => r.st_name == st_name).ToList().Count > 0)
                return true;
            else
                return false;
        } 
        
        /// 
        /// 被调用的方法
        /// 
        /// 
        /// 
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]//加上清除缓存
        public JsonResult CheckUserName(string st_name)
        {
            if (IsDistinctStName(st_name))
            {
                return Json("用户名不唯一", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }
        
        
        [HttpPost]
        public ActionResult Create(SchoolType schooltype)
        {
            //提交到服务器做一次判断
            if (IsDistinctStName(schooltype.st_name))
            {
                ModelState.AddModelError("st_name", "用户名称不是唯一的");
            }
            if (ModelState.IsValid)
            {
                db.SchoolTypes.Add(schooltype);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            return View(schooltype);
        }

 -----视图代码:

@model SchoolManageDomw.Models.SchoolType
@{
    ViewBag.Title = "Create";
}

Create

@using (Html.BeginForm()) {     @Html.ValidationSummary(true)     
        SchoolType                      @Html.LabelFor(model => model.st_name)         
                     @Html.EditorFor(model => model.st_name)             @Html.ValidationMessageFor(model => model.st_name)         
        

                     

     }
    @Html.ActionLink("Back to List", "Index")

 

***************************************************Compare验证

概要:

        如果需要比较验证,比如密码是否输入一致等,就可以用Compare验证

MVC3----数据注解与验证(2)之 详解Remote验证与Compare验证

 

实现:

 

-----模型代码:

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
    {
        [Key]
        public virtual int st_id { get; set; }
        [Required]  //不许为空
        public virtual string st_name{get;set;}
        [Compare("st_name")]
        public virtual string st_nameConfirm { get; set; }

        public virtual List Schools { get; set; }

    }
}

 

-----视图代码:

@model SchoolManageDomw.Models.SchoolType
@{
    ViewBag.Title = "Create";
}

Create

@using (Html.BeginForm()) {     @Html.ValidationSummary(true)     
        SchoolType                      @Html.LabelFor(model => model.st_name)         
                     @Html.EditorFor(model => model.st_name)             @Html.ValidationMessageFor(model => model.st_name)                                @Html.LabelFor(model => model.st_nameConfirm)                               @Html.EditorFor(model => model.st_nameConfirm)             @Html.ValidationMessageFor(model => model.st_nameConfirm)                  

                     

     }
    @Html.ActionLink("Back to List", "Index")

 


网站标题:MVC3----数据注解与验证(2)之详解Remote验证与Compare验证
链接URL:http://cxhlcq.com/article/jodhps.html

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部