2012-10-28 24 views
0

我有一個HTML.ActionLink在視圖上。我正在做的是我打電話給$.ajax()函數,它檢查從anction返回true或false。它會觸發該操作,並返回所需的結果true/false。但問題是它返回false時。我需要表現出alert和重定向應該只有在情況下,如果它的返回真..

ActionLink的:

<%: 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時纔會重定向...。

請糾正我在哪裏做錯了。

回答

2

您從AJAX回調中返回false。

這與來自外部函數的返回值無關; AJAX回調甚至不會在稍後纔開始運行。

+0

你可以解釋一下多一點,我很新MVC ..和我需要做的,以達到我目前的批准我的要求.. –

+0

@MayankPathak你的問題是clear.i已發佈一個答案對於這個問題,看起來除了這個之外你沒有更多的選擇。 –

+0

爲我工作.. + 1和接受爲答案:) –

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 
    } 

喝彩!

+0

試過..但不工作..它直接採取addList行動.. –

+0

我編輯it.check如果它的工作。 –