如果我尝试使用整数 AdditionalField,我不确定为什么我的验证不起作用。
这是我的代码(省略了部分代码):
模型
public class PersonViewModelBase
{
public int PersonID { get; set; }
[Required]
[StringLength(15, MinimumLength = 3)]
[Remote("Checkusername", "Utilities", AdditionalFields = "PersonID")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
public string UserName { get; set; }
public string Password { get; set; }
public string LastName { get; set; }
控制器(远程)
public JsonResult Checkusername(string username, int PersonID)
{
//var user = studentRepository.GetStudents()
// .Where(a => a.UserName == username.Trim())
// .Where(b => b.PersonID != personID);
var user = studentRepository.GetStudents().Where(a => a.UserName == username.Trim());
if (user.Count() > 0)
{
return Json(string.Format("{0} is not available.", username),
JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
未调用此远程验证。但是,如果我尝试将附加字段更改为字符串,应用程序将调用远程验证,尽管它会变为“未定义”,因为 PersonID 是一个整数