作form validation時,前端有時候會需要透過ajax檢查某些欄位,
例如會員註冊時,檢查此帳號是否已存在,在ASP.NET MVC可以這麼做達到前後端驗證
Controller:
public JsonResult IsUserAvailable(string createYear) { //驗證內容 return Json(result); }
在Modal中加入RemoteAttribute:
[Remote("IsUserAvailable", "UserId", ErrorMessage = "資料已存在", HttpMethod = "POST")] [Required] public string UserId { get; set; }
參考: How to: Implement Remote Validation in ASP.NET MVC