Question

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..

Was it helpful?

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.

OTHER TIPS

you must wait for your request to receive the result, and for doing this set async parameter of ajax function to false.

EDIT: you are lucky with your scenario. you can always return false and in case of successful delete call a function named DoRedirect.

this is the way to go :

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
  }

cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top