• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

清宇诺诺

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

MVC 模型js远程校验的使用方法

我们在网站注册的时候往往需要在用户注册完毕的时候显示用户名是否可用,这就要用到模型的远程校验了。具体如下。

     [Required(ErrorMessage = "用户名不能为空"), Remote("ValidateUserName", "CommonWebApi", AreaReference.UseRoot, ErrorMessage = "用户名已被占用")]
        [StringLength(100, ErrorMessage = "{0} 至少包含 {2} 个字符。", MinimumLength = 5)]
        [Display(Name = "用户名")]
        public string UserName { get; set; }
Remote方法实现了远程校验。

返回的类型为Bool

 /// <summary>
        /// 找回密码验证用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ActionResult VerifyUserName(string userName)
        {
            var user = _userProfileRepository.All().FirstOrDefault(item => item.UserName == userName);
            return Content(user == null ? "false" : "true");
        }

        public JsonResult GetAddressByGeo(decimal latitude,decimal longitude)
        {
            var address = _geoCodingInvoker.GetGeoAddressByCoordinate(latitude, longitude);
            return Json(address,JsonRequestBehavior.AllowGet);
        }

 


posted on 2015-06-08 11:54  清宇诺诺  阅读(282)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3