ASP.Net MVC3 远程数据注释不起作用
我有一个使用 Remote 属性进行数据注释的类:
public class Person
{
[Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
在 ValidationController 中:
public ActionResult NameValidation(string FirstName, string LastName)
{
bool isNameValid = true;
if (FirstName.Contains("John") && LastName.Contains("Doe"))
{
isNameValid = false;
}
return Json(isNameValid, JsonRequestBehavior.AllowGet);
}
在视图中,我有:
@{Html.EnableClientValidation(); }
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.FirstName) @Html.ValidationMessageFor(x => x.FirstName)
@Html.EditorFor(x => x.LastName) @Html.ValidationMessageFor(x => x.LastName)
<input name="finishButton" type="submit" id="button" >
}
如果我还添加了必需的属性,则仅会调用 NameValidation,如下所示:
public class Person
{
[Required]
[Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
如何让远程验证工作而不必拥有需要验证吗?
I have a class with data annotations using the Remote attribute:
public class Person
{
[Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
In ValidationController:
public ActionResult NameValidation(string FirstName, string LastName)
{
bool isNameValid = true;
if (FirstName.Contains("John") && LastName.Contains("Doe"))
{
isNameValid = false;
}
return Json(isNameValid, JsonRequestBehavior.AllowGet);
}
In the view I have:
@{Html.EnableClientValidation(); }
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.FirstName) @Html.ValidationMessageFor(x => x.FirstName)
@Html.EditorFor(x => x.LastName) @Html.ValidationMessageFor(x => x.LastName)
<input name="finishButton" type="submit" id="button" >
}
The NameValidation only gets called if I also add the Required attribute like this:
public class Person
{
[Required]
[Remote("NameValidation","Validation", ErrorMessage = "Field is Invalid", Fields = "LastName")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
How do I get the Remote validation to work without having to have the Required validation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新到 MVC3 RC2。我只是在没有“Required”属性的情况下尝试了它。它的工作。
但我发现奇怪的一件事是,有时每次按键时都会触发远程验证,而其他时候则会触发远程验证
Update to MVC3 RC2. I just tried it without the Required attribute. Its working.
One thing I found strange though, is that the remote validation is sometimes fired on every key press and onchange the other times