Search code examples
asp.net-mvc-3jqueryhtml.actionlink

HTML.ActionLink redirect not stopping with return false in $.ajax()


i've a HTML.ActionLink on view. what i'm doing is i'm making call to $.ajax() function which checks for return true or false from an anction. it hitting the action, returning the desired result true/false. but the problem is when it returns false. i need to show an alert and redirect should be only in case if its return true..

ActionLink:

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

Function:

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

            }
        });
    }

it redirects always..whether it returns true/false..it should redirect only if it returns true....

Please correct me where i'm doing wrong..


Solution

  • You're returning false from the AJAX callback.

    That has nothing to do with the return value from the outer function; the AJAX callback won't even start running until later.