ASP.NET MVC验证使用qTip jQuery插件

我正在使用找到的解决方案 here在使用qTip jQuery插件的工具提示中显示客户端验证错误.此解决方案非常适合客户端验证,但我希望能够以相同的方式显示服务器端验证错误.有谁知道如何利用qTip在工具提示中显示服务器端验证错误?

谢谢

解决方法

如果存在服务器端验证错误,当页面加载时会有一个带有“field-validation-error”类的span元素,因此我们可以简单地循环遍历该类的所有元素,提取内容或错误消息,并将其显示在工具提示中.
$(document).ready(function () {
    // Run this function for all validation error messages
    $('.field-validation-error').each(function () {

        // Get the name of the element the error message is intended for
        // Note: ASP.NET MVC replaces the '[',']',and '.' characters with an
        // underscore but the data-valmsg-for value will have the original characters
        var inputElem = '#' + $(this).attr('data-valmsg-for').replace('.','_').replace('[','_').replace(']','_');

        var corners = ['left center','right center'];
        var flipIt = $(inputElem).parents('span.right').length > 0;

        // Hide the default validation error
        $(this).addClass('Hidden');

        // Show the validation error using qTip
        $(inputElem).filter(':not(.valid)').qtip({                
            content: { text: $(this).text() },// Set the content to be the error message
            position: {
                my: corners[flipIt ? 0 : 1],at: corners[flipIt ? 1 : 0],viewport: $(window)
            },show: { ready: true },hide: false,style: { classes: 'ui-tooltip-red' }
        });            
    });
});

这是一个blog post,详细解释了如何做到这一点.

相关文章

引言 本文从Linux小白的视角, 在CentOS 7.x服务器上搭建一个...
引言: 多线程编程/异步编程非常复杂,有很多概念和工具需要...
一. 宏观概念 ASP.NET Core Middleware是在应用程序处理管道...
背景 在.Net和C#中运行异步代码相当简单,因为我们有时候需要...
HTTP基本认证 在HTTP中,HTTP基本认证(Basic Authenticatio...
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续...