参照地址:http://msdn.microsoft.com/en-us/library/gg508808(VS.98).aspx#Y2142
一、web.config加入 <appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
二、Controllers验证方法using System;using System.Globalization;using System.Web.Mvc;using System.Web.UI;using Mvc3RemoteVal.Models;
namespace Mvc3RemoteVal.Controllers { [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] //清除缓存 public class ValidationController : Controller {
/// <summary> /// 检验商家昵称是否已存在 /// </summary> /// <param name="sellerNick">昵称</param> /// <returns>bool</returns>
public JsonResult CheckSellerNick(string sellerNick) { bool isValidate = false; if (!SellerAccess.CheckSellerNick(sellerNick)) { isValidate = true; } return Json(isValidate, JsonRequestBehavior.AllowGet); }
}}三、modelpublic class CreateUserModel : EditUserModel { [Required] [StringLength(6, MinimumLength = 3)] [Remote("CheckSellerNick", "Seller",ErrorMessage="该昵称已存在")]//ActionName,Controller,错误信息 [RegularExpression(@"(/S)+", ErrorMessage = "White space is not allowed.")] [Editable(true)] public override string UserName { get; set; }}
四、View引用 <script src="../../Content/jquery/jquery-1.4.4.js" type="text/javascript"></script> <script src="../../Content/jquery/jquery.validate.min.js" type="text/javascript"></script> <script src="../../Content/jquery/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>