MVC Remote 伺服器驗證

SCLQ發表於2018-10-27

用此驗證必須在Controller中編寫返回值為JsonResult的Action

 1 public JsonResult CheckUserName(string UserName)
 2 {
 3   EFHelper<StudentDBEntities> dbContext = EFHelper<StudentDBEntities>.GetInstance();
 4   UserInfo result = dbContext.GetSingle<UserInfo>(item => item.UserName == UserName);
 5   bool isExists;
 6   if (result != null)
 7   {
 8     isExists = false;
 9     return Json(isExists, JsonRequestBehavior.AllowGet);
10   }
11   else
12   {
13   isExists = true;
14     return Json(isExists, JsonRequestBehavior.AllowGet);
15   }
16 
17 }

實體類程式碼:

1 [Remote("CheckUserName", "User", ErrorMessage = "使用者名稱已存在")]
2         public string UserName { get; set; }

 

相關文章