2012-10-28 27 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如果它的工作。 –