【问题标题】:HTML.ActionLink redirect not stopping with return false in $.ajax()HTML.ActionLink 重定向不会停止并在 $.ajax() 中返回 false
【发布时间】:2012-10-28 19:33:04
【问题描述】:

我有一个 HTML.ActionLink 正在查看。我正在做的是调用$.ajax() 函数,该函数检查来自anction 的返回真或假。它击中动作,返回所需的结果真/假。但问题是当它返回 false 时。我需要显示一个alert 并且重定向应该只在它返回 true 的情况下..

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) {

            }
        });
    }

它总是重定向..它是否返回真/假..只有当它返回真时才应该重定向....

请纠正我做错的地方..

【问题讨论】:

    标签: asp.net-mvc-3 jquery html.actionlink


    【解决方案1】:

    您从 AJAX 回调返回 false。

    这与外部函数的返回值无关; AJAX 回调直到稍后才会开始运行。

    【讨论】:

    • 你能再解释一下吗,我对 MVC 很陌生..以及我需要做些什么来达到我目前的要求..
    • @MayankPathak 你的问题很清楚。我已经发布了这个问题的答案,看来你没有其他选择了。
    【解决方案2】:

    您必须等待您的请求接收结果,并且为此将 ajax 函数的异步参数设置为 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
      }
    

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      相关资源
      最近更新 更多