1

私は ajax を使用していくつかの検証を行おうとしています。MVC3コントローラーからメソッドを呼び出しますが、コントローラーがtrueまたはfalseを返すかどうかにかかわらず、trueを返します。アラートは常に表示されます。

ClientChoices/HasJobInProgress

 public Boolean HasJobInProgress(int clientId)
        {
            return false;
            //return _proxy.GetJobInProgress(clientId);
        }

Jquery.Ajax

 $("#saveButton").click(function() {
        var hasCurrentJob =  
            $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: @Model.ClientId },
            success: function(data){
                return data;
            }
            });
        if (hasCurrentJob) {
            alert("The current clients has a job in progress. No changes can be saved until current job completes");
        } 
    });

解決

$("#saveButton").click(function() {
            $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: '@Model.ClientId' },
            success: function(data){
                showMsg(data);
            },
            cache: false
        });
    });

    function showMsg(hasCurrentJob) {
          if (hasCurrentJob.toString()=="True") {
               alert("The current clients has a job in progress. No changes can be saved until current job completes");
          } 
    }
4

1 に答える 1

3

AJAX 呼び出しは非同期であり、戻りデータは値を設定せずhasCurrentJob、成功コールバックに戻ります。に基づいてアラートを表示する方法については、以下を参照してくださいdata

    $("#saveButton").click(function() {
        //var hasCurrentJob
            $.ajax({
            url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
            data: { id: @Model.ClientId },
            success: function(data){
                showMsg(data);
            }
            });

    });

    function showMsg(hasCurrentJob) {
          if (hasCurrentJob === 'true') {
               alert("The current clients has a job in progress. No changes can be saved until current job completes");
          } 
    }
于 2012-04-10T14:20:59.933 に答える