自定义验证错误未显示
我有一个 MVC 3 应用程序,并尝试显示自定义验证错误。页面上会显示模型生成的正常验证错误,即“Required”。现在,我正在检查用户是否存在,如果存在,则添加一条错误消息:
if (userExists)
ModelState.AddModelError("UserName", UserManagementResources.UserAlreadyExistsText);
return View(model);
在视图上,我有一个验证摘要和一个 Html.ValidationMessage("UserName"),但两者都没有显示错误。我已经在其他页面成功使用了这个。我看到的与此页面的唯一区别是,它使用RequiredIf 验证器脚本。 http ://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx
任何如何解决这个问题的想法受到赞赏。谢谢。
编辑
我正在通过远程验证返回验证消息。如果我查看网络正在做什么,它会返回错误消息,但它仍然没有显示在视图上。
[Required]
[DataType(DataType.EmailAddress)]
[Remote("IsUserAvailable", "Validation", ErrorMessage = "Ein Benutzer mit dieser Email existiert bereits.")]
[Display(Name = Resources.EmailText, ResourceType = typeof(Resources))]
public string Email
{
get { return User.Email; }
set { User.Email = value; }
}
视图:
@Html.LabelFor(u => u.Email, Resources.Email + " (Login) *")
@Html.EditorFor(u => u.Email)
@Html.ValidationMessageFor(u => u.Email)
<br clear="all" />
远程验证控制器:
public class ValidationController : Controller
{
public JsonResult IsUserAvailable(string Email)
{
bool userExists;
using (var userModel = new UserManagementModel())
{
userExists = userModel.UserExists(Email);
}
if(userExists)
return Json(UserManagementResources.UserAlreadyExists, JsonRequestBehavior.AllowGet);
else
return Json(true, JsonRequestBehavior.AllowGet);
}
}
I have an MVC 3 application and am trying to display a custom validation error. The normal validation errors that are generated by the model, i.e. Required, are displayed on the page. Now I am checking if a user exists and if so, adding a error message:
if (userExists)
ModelState.AddModelError("UserName", UserManagementResources.UserAlreadyExistsText);
return View(model);
On the view I have a validation summary and a Html.ValidationMessage("UserName"), but neither one is displaying the error. I have used this successfully on other pages. The only difference with this page I can see is, that it uses the RequiredIf validator scripts.
http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx
Any ideas how to solve this problem are appreciated. Thanks.
Edit
I am returning the validation message through the Remote validation. If I look what the network is doing, it's returning the error message, but it is still not displayed on the view.
[Required]
[DataType(DataType.EmailAddress)]
[Remote("IsUserAvailable", "Validation", ErrorMessage = "Ein Benutzer mit dieser Email existiert bereits.")]
[Display(Name = Resources.EmailText, ResourceType = typeof(Resources))]
public string Email
{
get { return User.Email; }
set { User.Email = value; }
}
The View:
@Html.LabelFor(u => u.Email, Resources.Email + " (Login) *")
@Html.EditorFor(u => u.Email)
@Html.ValidationMessageFor(u => u.Email)
<br clear="all" />
The Remote Validation Controller:
public class ValidationController : Controller
{
public JsonResult IsUserAvailable(string Email)
{
bool userExists;
using (var userModel = new UserManagementModel())
{
userExists = userModel.UserExists(Email);
}
if(userExists)
return Json(UserManagementResources.UserAlreadyExists, JsonRequestBehavior.AllowGet);
else
return Json(true, JsonRequestBehavior.AllowGet);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用远程验证为此?
为什么回发只是为了检查用户是否存在?
示例:
并创建一个具有
UserNameExists
方法的验证控制器,例如Why don't you use the Remote validation for this?
Why posting back just to check if user exists?
example:
and create a Validation Controller having the
UserNameExists
method like当您更改 jQuery.js 的版本时,您还必须更改validation.js 文件。不同的版本彼此不兼容,当您混合文件时,您可能会在不同的浏览器中看到奇怪的行为。
When you change the version of your jQuery.js you have to change the validation.js file as well. Different versions are not compatible to each other and you might see strange behaviour in different browsers when you mixup the files.