繁体   English   中英

ASP.Net MVC:如何将简洁的验证消息附加到控件

[英]ASP.Net MVC: How to attach unobtrusive validation message to controls

而不是显示验证消息,我想将验证消息附加到控件标题属性,并且我已经完成了,但是没有显示标题属性的工具提示,并且验证失败时也不会出现红色边框。

在这里,我将附加一些小的示例代码,然后将验证消息附加到controls属性。

$("span[class='field-validation-error']").each(function () {

$(this).addClass("hidden");//Add class hidden to hide  @@Html.ValidationMessageFor(model => model.xyz) if using bootstrap , else use css
            var inputID = $(this).attr("data-valmsg-for");//get the id of the input field for which this validation prompted
            var validationMessage = $(this).html();//Get validation message for input filed which is prompted          
            //$("#" + inputID).tooltip({ 'trigger': 'hover', 'title': validationMessage });//Trigger the tooltip now, if using bootstrap.

                      //******OR*******

            $("#" + inputID).attr("title",validationMessage);
              });           
        }
        return false;
    });

我完整的jquery和mvc代码在dotnet小提琴中,其链接为https://dotnetfiddle.net/30YQEX

所以请有人检查代码并与我讨论我在那里做错了什么?

1)为什么将鼠标悬停在控件上会导致验证失败,为什么工具提示不出现?

2)为什么验证失败时红色边框不会出现?

谢谢

您的$("span[class='field-validation-error']").each ...代码在打开页面时执行。 而是应在验证发生后执行它。 只有这样,错误css类才会应用于控件。

最好的方法是重写jQuery Validate默认事件:

$.validator.setDefaults({
    highlight: function (element) {
        $(element).   //do your custom error displaying here
    },
    unhighlight: function (element) {
        //hide your custom error here
    },
    ignore: [],
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM