我正在使用我的MVC模型中的Remote属性进行远程验证,请找到以下代码:
[Required]
[System.Web.Mvc.Remote("IsEmailExist", "Account", HttpMethod = "POST", ErrorMessage = "The Email Already Exists")]
在控制器动作方法中,我使用电子邮件的用户输入作为参数并检查数据库,请找到以下代码:
public JsonResult IsEmailExist(string emailAddress)
{
using (var db = new YouTubeNZ())
{
var isExist = !db.Users.Any(X => X.EmailAddress == emailAddress);
return Json(isExist, JsonRequestBehavior.AllowGet);
}
}
但是在运行时,action方法中的参数是" Null"当值应该是用户输入的电子邮件地址时,并且未对现有电子邮件进行验证。
答案 0 :(得分:0)
确保使用HttpPost
attr修饰您的操作方法,并且您的属性名称与方法参数匹配(即:EmailAddress
):
[HttpPost]
public JsonResult IsEmailExist(string emailAddress)
{
using (var db = new YouTubeNZ())
{
var isExist = !db.Users.Any(X => X.EmailAddress == emailAddress);
return Json(isExist, JsonRequestBehavior.AllowGet);
}
}
答案 1 :(得分:0)
请确保以下内容。
您的模型属性应该像
[Required]
[System.Web.Mvc.Remote("IsEmailExist", "Account", ErrorMessage = "The Email Already Exists")]
public string EmailAddress { get; set; }
同时将AccountsController操作方法更改为
public ActionResult IsEmailExist(string emailAddress)
{
using (var db = new YouTubeNZ())
{
bool isExist = !db.Users.Any(X => X.EmailAddress == emailAddress);
return Json(isExist, JsonRequestBehavior.AllowGet);
}
}
答案 2 :(得分:0)
Your Model should be like this
[Required]
[Remote("IsEmailExist","Account",ErrorMessage="This Email is already exists")]
public string EmailAddress{get;set;}
And Controller should be like this
public JsonResult IsEmailExist(string emailAddress)
{
return Json(isExist(emailAddress),JsonRequestBehavior.AllowGet);
}
private bool isExist(string emailAddress)
{
using (var db = new YouTubeNZ())
{
return !db.Users.Any(X => X.EmailAddress == emailAddress);
}
}