English 中文(简体)
MVC习俗的不侵犯性认证始终显示信息
原标题:MVC custom unobtrusive validation always showing message

我在创造不侵犯习俗的验证方面说得相当新鲜,而且正在陷入一个棘手的问题。

这一验证基本上只是对数据库(通过Ajax)进行核对,以了解模型和序列号组合是否已经存在。 j和Ajax似乎正在按预期行事,但我发出的验证信息一经要求就显现出来。

这里是我的 j:

$.validator.addMethod( newserialandmodel ,
    function (value, element, parameters) {
        var modelNumber = $( #ProductInformation_ModelNumber ).val();
        var serialNumber = value;

        var token = $( input[name=__RequestVerificationToken] ).val();

        $.ajax({
            type: "POST",
            url: "/ajax/getmodelandserialexists/",
            data: ({ 
                    modelNumber: modelNumber, 
                    serialNumber: serialNumber, 
                     __RequestVerificationToken : token 
                  }),
            cache: true,
            dataType:  json ,
            success: function (response) {
                if (response.exists) {
                    $.validator.messages.newserialandmodel = 
                        $.format($.validator.messages.newserialandmodel);
                }

                return !response.exists;
            }
        });

        return false;
    }
);

$.validator.unobtrusive.adapters.add(
     newserialandmodel ,
    function (options) {
        options.rules[ newserialandmodel ] = options.params;

        if (options.message != null) {
            $.validator.messages.newserialandmodel = options.message;
        }
    }
);

形式要素如下:

<input type="text" value="" 
 name="ProductInformation.SerialNumber" id="ProductInformation_SerialNumber" 
 data-val-required="The Serial number field is required." 
 data-val-newserialandmodel="已订有提供模型和序列号的合同。" 
 data-val-length-max="30" 
 data-val-length="The 30 field requires no more than 30 characters." 
 data-val="true" class="input-validation-error">

当我走过一步时,一旦打上了第一个验证线,信息就已经显示:

已订有提供模型和序列号的合同。

Also, even though the function returns true, it the message remains.

其余职能/Ajax的工作只是罚款,我可以指出,它为什么要立即展示这一信息,为什么在我回去的时候,它会留在那里。

有什么想法?

最佳回答

由于格雷瓜和零斯的回复,我不得不在轮椅上玩.。

The problem is that the return within my Ajax success function just returns the response back to the success function, not the validation function. So I had to change it to set a value within the Ajax success function, and return that.

It s also worth noting that I had to include async: false, as otherwise the validation function will return (the default value of false) before the Ajax call completes.

$.validator.addMethod( newserialandmodel ,
    function (value, element, parameters) {
        var modelNumber = $( #ProductInformation_ModelNumber ).val();
        var serialNumber = value;

        var token = $( input[name=__RequestVerificationToken] ).val();

        var isValid = false;

        $.ajax({
            type: "POST",
            url: "/ajax/getmodelandserialexists/",
            data: ({ 
                    modelNumber: modelNumber, 
                    serialNumber: serialNumber, 
                     __RequestVerificationToken : token }),
            async: false,
            dataType:  json ,
            success: function (response) {
                if (response.exists) {
                    $.validator.messages.newserialandmodel = 
                        $.format($.validator.messages.newserialandmodel);
                }

                isValid = !response.exists;
            }
        });

        return isValid;
    }
);
问题回答

我认为,这是因为你在任期结束时正在返回<代码>false。 因此,该功能将退回错误,然后将<条码>success试图返回<条码>的背式火焰(<>true>,但时间太晚,<条码>已退回到有效方。 简单的解决办法是利用<条码>sync:虚假<>条码>,使您的美国航天署发出同步呼吁。

couple:

  1. 你有“切身:真的”,这意味着如果URL是一样的,它不会从服务器上跳出。 我不知道是否检查了持久性有机污染物的参数,因此,即便有不同的参数,也有可能使用该清单。 setting笑.。

  2. 我不熟悉改编者APIC,但能否使以前的活动满足条件? (这些选择在实施时实际上不是无效的)?





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签