自定义验证错误未显示

发布于 2024-11-08 11:01:14 字数 1794 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-11-15 11:01:14

为什么不使用远程验证为此?

为什么回发只是为了检查用户是否存在?

示例:

public class RegisterModel
{
    [Required]
    [Remote("UserNameExists", "Validation", "", ErrorMessage = "Username is already taken.")]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
    [Display(Name = "Username")]
    public string UserName { get; set; }
}

并创建一个具有 UserNameExists 方法的验证控制器,例如

public JsonResult UserNameExists(string UserName)
{
    var user = _db.Users.Where(x => x.username.Equals(UserName));

    return user == null ?
        Json(true, JsonRequestBehavior.AllowGet) :
        Json(string.Format("{0} is not available.", register.UserName), JsonRequestBehavior.AllowGet);
}

Why don't you use the Remote validation for this?

Why posting back just to check if user exists?

example:

public class RegisterModel
{
    [Required]
    [Remote("UserNameExists", "Validation", "", ErrorMessage = "Username is already taken.")]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
    [Display(Name = "Username")]
    public string UserName { get; set; }
}

and create a Validation Controller having the UserNameExists method like

public JsonResult UserNameExists(string UserName)
{
    var user = _db.Users.Where(x => x.username.Equals(UserName));

    return user == null ?
        Json(true, JsonRequestBehavior.AllowGet) :
        Json(string.Format("{0} is not available.", register.UserName), JsonRequestBehavior.AllowGet);
}
め七分饶幸 2024-11-15 11:01:14

当您更改 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文