您好,登錄后才能下訂單哦!
***************************************************Remote驗(yàn)證
概要:
如果要實(shí)現(xiàn)像用戶注冊那樣,不允許出現(xiàn)重復(fù)的賬戶,就可以用到Remote驗(yàn)證。Remote特性允許利用服務(wù)器端的回調(diào)函數(shù)執(zhí)行客戶端的驗(yàn)證邏輯。它只是在文本框中輸入字符的時(shí)候向服務(wù)器提交get請求,Remote驗(yàn)證只支持輸入的時(shí)候驗(yàn)證,不支持提交的時(shí)候驗(yàn)證,這存在一定的安全隱患。所以我們要在提交的時(shí)候也要驗(yàn)證,驗(yàn)證失敗了,就添加上ModelError
實(shí)現(xiàn):
-------模型代碼:
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; } // 要調(diào)用的方法 控制器名稱 [Remote("CheckUserName", "SchoolType")]//Remote驗(yàn)證 public virtual string st_name{get;set;} public virtual List<School> Schools { get; set; } } }
-------控制器代碼:
需要的名稱控件:
using System.Web.Security;
using System.Web.UI;
private SchoolDBContext db = new SchoolDBContext(); /// <summary> /// 定義一個(gè)方法,做唯一判斷 /// </summary> /// <param name="st_name"></param> /// <returns></returns> private bool IsDistinctStName(string st_name) { if (db.SchoolTypes.Where(r => r.st_name == st_name).ToList().Count > 0) return true; else return false; } /// <summary> /// 被調(diào)用的方法 /// </summary> /// <param name="st_name"></param> /// <returns></returns> [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) { //提交到服務(wù)器做一次判斷 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"; } <h3>Create</h3> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>SchoolType</legend> <div class="editor-label"> @Html.LabelFor(model => model.st_name) </div> <div class="editor-field"> @Html.EditorFor(model => model.st_name) @Html.ValidationMessageFor(model => model.st_name) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
***************************************************Compare驗(yàn)證
概要:
如果需要比較驗(yàn)證,比如密碼是否輸入一致等,就可以用Compare驗(yàn)證
實(shí)現(xiàn):
-----模型代碼:
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<School> Schools { get; set; } } }
-----視圖代碼:
@model SchoolManageDomw.Models.SchoolType @{ ViewBag.Title = "Create"; } <h3>Create</h3> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>SchoolType</legend> <div class="editor-label"> @Html.LabelFor(model => model.st_name) </div> <div class="editor-field"> @Html.EditorFor(model => model.st_name) @Html.ValidationMessageFor(model => model.st_name) </div> <div class="editor-label"> @Html.LabelFor(model => model.st_nameConfirm) </div> <div class="editor-field"> @Html.EditorFor(model => model.st_nameConfirm) @Html.ValidationMessageFor(model => model.st_nameConfirm) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。