欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET MVC5驗(yàn)證系列之Remote Validation

 更新時(shí)間:2016年07月28日 14:49:23   作者:灰太狼的夢(mèng)想  
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之Remote Validation,感興趣的小伙伴們可以參考一下

大多數(shù)的開(kāi)發(fā)者,可能會(huì)遇到這樣的情況:當(dāng)我們?cè)趧?chuàng)建用戶(hù)之前,有必要去檢查是否數(shù)據(jù)庫(kù)中已經(jīng)存在相同名字的用戶(hù)。換句話說(shuō)就是,我們要確保程序中,只有一個(gè)唯一的用戶(hù)名,不能有重復(fù)的。相信大多數(shù)人都有不同的解決方法,但是ASP.NET MVC中,為我們提供了一個(gè)特性,就是Remote Validation,用它可以解決類(lèi)似這樣的問(wèn)題。

Remote Validation調(diào)用了一個(gè)Ajax請(qǐng)求,可以是GET或者POST方式,接著調(diào)用方法,這個(gè)方法,至少要有一個(gè)參數(shù),并且方法的返回類(lèi)型是Json格式的。【MVC中通過(guò)JSOnResult來(lái)做到】,這個(gè)方法的參數(shù)就是要驗(yàn)證的實(shí)體的屬性【必須,否則不能驗(yàn)證,參數(shù)的大小寫(xiě)無(wú)所謂。】,如果這個(gè)驗(yàn)證的方法返回值是true,那么就表名存在相同的用戶(hù),我們就返回false,給前臺(tái)頁(yè)面。表明驗(yàn)證不通過(guò)。

好了,直接說(shuō)正題吧!
首先新建一個(gè)空白的MVC項(xiàng)目,在Model文件夾下,新建一個(gè)類(lèi)RemoteUser: 

public class RemoteUser
 { 
 public string Name { get; set; } 
 public string Email { get; set; }
 
 }

然后建一個(gè)測(cè)試的數(shù)據(jù)類(lèi): 

 public static class MyRemoteStaticData
 {
 public static List<RemoteUser> RemoteList
 {
 get
 {
 return new List<RemoteUser>()
 {
 new RemoteUser(){Name="Daniel",Email="Daniel@163.com"},
 new RemoteUser(){Name="CFS",Email="CFS@163.com"}
 };
 }

 }
 }

 

在新建一個(gè)控制器MyRemoteController 【主要用來(lái)Remote驗(yàn)證】:

using Server_Side_Validation_IN_MVC.StaticData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Server_Side_Validation_IN_MVC.Controllers
{
 public class MyRemoteController : Controller
 {
 // GET: MyRemote
 public JsonResult RemoteValidate(string name) //這里的參數(shù)名字,必須要和視圖中文本框控件的名字一樣,但大小寫(xiě)無(wú)所謂
 {
 //如果存在用戶(hù)名,即isExists=true
 bool isExists = MyRemoteStaticData.RemoteList.
 Where(s => s.Name.ToLowerInvariant().
  Equals(name.ToLower())).FirstOrDefault() != null;
 //就向前臺(tái)返回false,表明已經(jīng)存在userName
 return Json(!isExists,JsonRequestBehavior.AllowGet);
 }

 

 
}

上面添加完驗(yàn)證之后,我們來(lái)修改一下Model實(shí)體:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Server_Side_Validation_IN_MVC.Models
{
 public class RemoteUser
 {
 [Remote("RemoteValidate", "MyRemote", ErrorMessage = "抱歉用戶(hù)名已經(jīng)存在!請(qǐng)重新輸入!?。?)]
 public string Name { get; set; }

 
 public string Email { get; set; }

 
 }
}

然后在新建一個(gè)測(cè)試的控制器: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Server_Side_Validation_IN_MVC.Controllers
{
 public class UserController : Controller
 {
 

 public ActionResult AddRemoteUser()
 {
 return View();
 }
 }
}

添加AddRemoteUser視圖,【注意這里,Remote Validation是需要引入Jquery插件和啟用客戶(hù)端驗(yàn)證的】

這里勾選引入腳本庫(kù),也主要是用來(lái)引入Jquery插件。 

@model Server_Side_Validation_IN_MVC.Models.RemoteUser

@{
 ViewBag.Title = "AddRemoteUser";
}

<h2>AddRemoteUser</h2>


@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 
 <div class="form-horizontal">
 <h4>RemoteUser</h4>
 <hr />
 @Html.ValidationSummary(true, "", new { @class = "text-danger" })
 <div class="form-group">
 @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
 </div>
 </div>

 

 <div class="form-group">
 <div class="col-md-offset-2 col-md-10">
 <input type="submit" value="Create" class="btn btn-default" />
 </div>
 </div>
 </div>
}

<div>
 @Html.ActionLink("Back to List", "Index")
</div>

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

然后修改一下默認(rèn)的路由: 

public static void RegisterRoutes(RouteCollection routes)
 {
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 routes.MapRoute(
 name: "Default",
 url: "{controller}/{action}/{id}",
 defaults: new { controller = "User", action = "AddRemoteUser", id = UrlParameter.Optional }
 );
 }

運(yùn)行項(xiàng)目:

輸入測(cè)試數(shù)據(jù):CFS,按Tab鍵后,自動(dòng)就進(jìn)行驗(yàn)證了。 

這里我們對(duì)Name字段就進(jìn)行了Remote驗(yàn)證,現(xiàn)在我想對(duì)Email字段進(jìn)行驗(yàn)證,需要使用到AdditionalFields,屬性,還需要另外添加一個(gè)驗(yàn)證方法: 

using Server_Side_Validation_IN_MVC.StaticData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Server_Side_Validation_IN_MVC.Controllers
{
 public class MyRemoteController : Controller
 {
 // GET: MyRemote
 public JsonResult RemoteValidate(string name) //這里的參數(shù)名字,必須要和視圖中文本框控件的名字一樣,但大小寫(xiě)無(wú)所謂
 {
 //如果存在用戶(hù)名,即isExists=true
 bool isExists = MyRemoteStaticData.RemoteList.
 Where(s => s.Name.ToLowerInvariant().
  Equals(name.ToLower())).FirstOrDefault() != null;
 //就向前臺(tái)返回false,表明已經(jīng)存在userName
 return Json(!isExists,JsonRequestBehavior.AllowGet);
 }


 public JsonResult RemoteValidationAddtional(string name, string email)
 {
 //如果存在用戶(hù)名,即isExists=true
 bool isExists = MyRemoteStaticData.RemoteList.
 Where(s => s.Name.ToLowerInvariant().
  Equals(name.ToLower()) && s.Email.ToLowerInvariant().Equals(email.ToLower())).FirstOrDefault() != null;
 //就向前臺(tái)返回false,表明已經(jīng)存在userName
 return Json(!isExists, JsonRequestBehavior.AllowGet);
 }
 }
}

 

然后修改對(duì)應(yīng)的實(shí)體類(lèi): 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Server_Side_Validation_IN_MVC.Models
{
 public class RemoteUser
 {
 [Remote("RemoteValidate", "MyRemote", ErrorMessage = "抱歉用戶(hù)名已經(jīng)存在!請(qǐng)重新輸入?。?!")]
 public string Name { get; set; }

 //注意,這里的AdditionalFields="Name",Name字段必須和Modle中的字段完全一樣
 [Remote("RemoteValidationAddtional", "MyRemote", AdditionalFields = "Name", ErrorMessage = "抱歉Email已經(jīng)存在!請(qǐng)重新輸入?。?!")]
 public string Email { get; set; }

 } 
 }

接著運(yùn)行項(xiàng)目:

輸入在測(cè)試類(lèi)中寫(xiě)的測(cè)試數(shù)據(jù):

這里就對(duì)兩個(gè)字段進(jìn)行了Remote Validation了。
上面使用了AdditionalFields 驗(yàn)證字段,如果我們想要驗(yàn)證不只一個(gè)字段,可以在AddtionalFiled里面添加,以逗號(hào)分隔就行了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫(xiě)入、保存)

    ASP.NET對(duì)txt文件相關(guān)操作(讀取、寫(xiě)入、保存)

    這篇文章主要介紹了ASP.NETtxt文件相關(guān)操作,包括讀取、寫(xiě)入、保存,需要的朋友可以參考下
    2015-09-09
  • .NET內(nèi)存泄漏分析Windbg項(xiàng)目實(shí)例

    .NET內(nèi)存泄漏分析Windbg項(xiàng)目實(shí)例

    這篇文章介紹了.NET內(nèi)存泄漏分析Windbg項(xiàng)目實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • ASP.Net Post方式獲取數(shù)據(jù)流的一種簡(jiǎn)單寫(xiě)法

    ASP.Net Post方式獲取數(shù)據(jù)流的一種簡(jiǎn)單寫(xiě)法

    這篇文章主要介紹了ASP.Net Post方式獲取數(shù)據(jù)流的一種簡(jiǎn)單寫(xiě)法,本文直接給出代碼實(shí)例,需要的朋友可以參考下
    2015-05-05
  • ASP.Net?Core中的日志與分布式鏈路追蹤

    ASP.Net?Core中的日志與分布式鏈路追蹤

    這篇文章介紹了ASP.Net?Core中的日志與分布式鏈路追蹤,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下<BR>
    2022-03-03
  • Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼

    Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼

    這篇文章主要介紹了Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼,本文一并列出了javascript語(yǔ)言的實(shí)現(xiàn)方法,用以實(shí)現(xiàn)字符串和HTML十進(jìn)制編碼之間互相轉(zhuǎn)換功能,需要的朋友可以參考下
    2014-08-08
  • VS2010新建站點(diǎn)發(fā)布并訪問(wèn)步驟詳解

    VS2010新建站點(diǎn)發(fā)布并訪問(wèn)步驟詳解

    這篇文章主要介紹了VS2010新建站點(diǎn)發(fā)布并訪問(wèn)詳細(xì)步驟,主要以具體每個(gè)步驟截圖的形式從站點(diǎn)新建,配置IIS再到發(fā)布和訪問(wèn)網(wǎng)站進(jìn)行講解,需要的朋友可以參考下
    2015-08-08
  • .Net Core日志記錄之日志配置

    .Net Core日志記錄之日志配置

    這篇文章介紹了.Net Core日志記錄之日志配置,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 防止重復(fù)提交 僅提交一次的終極絕殺技

    防止重復(fù)提交 僅提交一次的終極絕殺技

    防止重復(fù)提交,通用的思路,就是當(dāng)用戶(hù)點(diǎn)擊提交按鈕后,在瀏覽器中用JS將按鈕disable掉,從而阻止用戶(hù)繼續(xù)點(diǎn)擊該按鈕,實(shí)現(xiàn)防止重復(fù)提交的目的。網(wǎng)上防止重復(fù)提交的文章已經(jīng)不少了,為啥我還要寫(xiě)呢,顯然我不是吃飽了撐的。。。
    2010-08-08
  • ASP.NET?Core依賴(lài)注入詳解

    ASP.NET?Core依賴(lài)注入詳解

    本文詳細(xì)講解了ASP.NET?Core的依賴(lài)注入,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能

    Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能

    在項(xiàng)目中經(jīng)常有代碼在線編輯的需求,比如修改基于Xml的配置文件,編輯Json格式的測(cè)試數(shù)據(jù)等。這篇文章主要介紹了Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能,需要的朋友可以參考下
    2022-01-01

最新評(píng)論