0

私はオンビューHTML.ActionLinkです。私がやっていることは$.ajax()、からの戻り値 true または false をチェックする関数を呼び出すことですanction。アクションをヒットし、目的の結果の true/false を返します。しかし、問題は false を返すときです。を表示する必要がありalert、リダイレクトは、true が返された場合にのみ行う必要があります。

アクションリンク:

<%: Html.ActionLink("Add Race", "AddRace",
     new {eventId = Model.EventId, fleetId=Model.SelectedFleet.ID}, 
          new{onclick="return checkFleetAddedandScroing()"}) %>

関数:

 function checkFleetAddedandScroing() {
        debugger;
        $.ajax({
            type: "GET",
            url: '<%=Url.Action("CheckFleetExists", new {eventId=Model.EventId})%>',
            dataType: "json",
            cache: false,
            success: function (data, textStatus) {
                data = eval("(" + data + ")");
                if (data == true) {
                    alert('Ok button clicked');
                    return true;
                }
                else {
                    alert("Cannot delete this fleet becasue either you have already added races to this event or the fleet has used for boat registration.");
                    return false;
                }
            }, //success
            error: function (req) {

            }
        });
    }

それは常にリダイレクトします..true / falseを返すかどうか..trueを返す場合にのみリダイレクトする必要があります....

私が間違っているところを修正してください..

4

2 に答える 2

2

AJAX コールバックから false を返しています。

これは、外部関数からの戻り値とは関係ありません。AJAX コールバックは、後になるまで実行を開始しません。

于 2012-10-28T19:38:50.550 に答える
1

リクエストが結果を受け取るまで待つ必要があります。これを行うには、ajax 関数の async パラメータを false に設定します。

編集:あなたのシナリオは幸運です。いつでも false を返すことができ、削除が成功した場合は という名前の関数を呼び出しますDoRedirect

これが行く方法です:

function checkFleetAddedandScroing() {
        debugger;
        $.ajax({
            type: "GET",
            url: '<%=Url.Action("CheckFleetExists", new {eventId=Model.EventId})%>',
            dataType: "json",        
            timeout: 30000,
            cache: false,
            success: function (data, textStatus) {
                data = eval("(" + data + ")");
                if (data == true) {
                    alert('Ok button clicked'); 
                    DoRedirect();
                }
                else {
                    alert("Cannot delete this fleet becasue either you have already added races to this event or the fleet has used for boat registration.");
                }
            }, //success
            error: function (req) {

            }
        });
        return false;
    }

  function DoRedirect(){
        //code for do redirect
  }

乾杯!

于 2012-10-28T19:46:20.407 に答える