工具提示中的 MVC3 验证错误消息

发布于 2025-01-06 22:57:58 字数 852 浏览 0 评论 0原文

我是 ASP.NET 和 MVC3 的新手。我有一个项目,其中需要在工具提示中显示 MVC3 验证错误消息而不更新 jquery.validate.unobtrusive.js。 我已经尝试过这个

单击此处

但无法使其正常工作。这是我应该根据上面的链接使用的功能。它说在这里做一些事情...我不知道应该编写什么代码来在验证失败时在必填字段旁边显示带有错误图标的错误消息。

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    var oldErrorFunction = settngs.errorPlacement;
    var oldSucessFunction = settngs.success;
    settngs.errorPlacement = function (error, inputElement) {
        //Do something here
        oldErrorFunction(error, inputElement);
    }
    settngs.success = function (error) {
        //Do something here
        oldSucessFunction(error);
    }
});

有人可以帮忙吗?

I am new to ASP.NET and MVC3. I have got a project where the MVC3 Validation Error Message needs to be shown in Tooltip without updating jquery.validate.unobtrusive.js.
I have tried this

click here

But could not get it to work. Here is the function the I should be using according to the link above. It says do something here...I could not figure out what code I should write to show the error message with a error icon beside the required field when validation fails.

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    var oldErrorFunction = settngs.errorPlacement;
    var oldSucessFunction = settngs.success;
    settngs.errorPlacement = function (error, inputElement) {
        //Do something here
        oldErrorFunction(error, inputElement);
    }
    settngs.success = function (error) {
        //Do something here
        oldSucessFunction(error);
    }
});

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

这样的小城市 2025-01-13 22:57:58

您可以使用以下CSS,

.myfield-validation-error
{
    content: "";
    display: inline-block;
    height: 16px;
    width: 16px;
    margin-right: 4px;
    background-image:url(../../Images/Close-2-icon.png);
}
.myfield-validation-valid
{
    content: "";
    display: inline-block;
    height: 16px;
    width: 16px;
    margin-right: 4px;
    background-image:url(../../Images/Ok-icon.png);
}

然后您可以在成功和失败回调中使用这些类,

<script>
    $(function () {
        var settngs = $.data($('form')[0], 'validator').settings;
        var oldErrorFunction = settngs.errorPlacement;
        var oldSucessFunction = settngs.success;
        settngs.errorPlacement = function (error, inputElement) {
            $(inputElement).closest('div').next().removeClass('myfield-validation-valid').addClass('myfield-validation-error')
            oldErrorFunction(error, inputElement);
        }
        settngs.success = function (error) {
            $(error).closest('div').addClass('myfield-validation-valid').removeClass('myfield-validation-error')
            oldSucessFunction(error);
        }
    });
</script>

进一步检查 链接。

You can use the following css,

.myfield-validation-error
{
    content: "";
    display: inline-block;
    height: 16px;
    width: 16px;
    margin-right: 4px;
    background-image:url(../../Images/Close-2-icon.png);
}
.myfield-validation-valid
{
    content: "";
    display: inline-block;
    height: 16px;
    width: 16px;
    margin-right: 4px;
    background-image:url(../../Images/Ok-icon.png);
}

Then you can make use your these classes on your success and failure callback,

<script>
    $(function () {
        var settngs = $.data($('form')[0], 'validator').settings;
        var oldErrorFunction = settngs.errorPlacement;
        var oldSucessFunction = settngs.success;
        settngs.errorPlacement = function (error, inputElement) {
            $(inputElement).closest('div').next().removeClass('myfield-validation-valid').addClass('myfield-validation-error')
            oldErrorFunction(error, inputElement);
        }
        settngs.success = function (error) {
            $(error).closest('div').addClass('myfield-validation-valid').removeClass('myfield-validation-error')
            oldSucessFunction(error);
        }
    });
</script>

Further check this link.

停顿的约定 2025-01-13 22:57:58

如果您使用 MVC3,则可以利用提供的验证。查看帐户控制器 - 注册视图,了解如何实现它的示例。

研究(查看中):

@Html.ValidationSummary(true, "帐户创建失败。
请更正错误并重试。”)

以及以下类别:

System.ComponentModel.DataAnnotations.RequiredAttribute

If you're using MVC3, you can leverage the validation provided. Take a look at the Account Controller - Register View for an example of how to implement it.

Research (in View):

@Html.ValidationSummary(true, "Account creation was unsuccessful.
Please correct the errors and try again.")

and the Class for:

System.ComponentModel.DataAnnotations.RequiredAttribute

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