我遇到了远程验证问题。
我有一个带有属性的视图模型,我在该属性上添加了一个远程验证器,但是当我运行表单并在文本框中输入一个字符串时,传递给控制器的值为空。
viewmodel 中的属性如下所示:
[Required(ErrorMessage = "Enter the host's name")]
[Remote("ValidateHostFullName", "BoardroomBooking", ErrorMessage = "Enter a different name")]
[DisplayName("Host's Name")]
public string HostFullName { get; set; }
Controller 中验证器的代码如下所示:
public ActionResult ValidateHostFullName([Bind(Prefix="BookingReceptionViewModel")]string HostFullName)
{
if (!HostFullName.Equals("John Smith"))
{
return Json(true, JsonRequestBehavior.AllowGet);
}
return Json("{0} is not allowed", JsonRequestBehavior.AllowGet);
}
无论在框中键入什么,HostFullName 的字符串值都显示为 null。我已经尝试过使用和不使用绑定前缀,这没有区别。
我已经在模型上尝试过它并且它有效,它似乎只有在我使用视图模型时才会出现问题。
谢谢
标记