参照地址:http://msdn.microsoft.com/en-us/library/gg508808%28VS.98%29.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);
    
        }

    }
}
三、model
public 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="http://www.cnblogs.com/Content/jquery/jquery-1.4.4.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/Content/jquery/jquery.validate.min.js" type="text/javascript"></script>
    <script src="http://www.cnblogs.com/Content/jquery/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

 posted on 2012-10-09 11:08  叶落☆无声  阅读(1649)  评论(0编辑  收藏  举报