Learn AspNet
Learn AspNet

Reputation: 1622

Pass in parameter from ajax success response to Url Action method

I have a ajax call to an Api that adds the data to database, I would like success function to call a controller with action method to different with parameter received from response of API call. How can I do that

Ajax Call

 $.ajax({
            //Url should contain the method you want to run
            url: "/api/ApiTest",
            //Method will be one of the REST API verb
            method: "POST",
            //These are all the parameters to be passed to method for rest api
            data:viewModel,
            dataType: 'json',
            success: function (data) {
                var bookingIDParam = data.Booking.BookingID
                window.location.href = '@Url.Action("BookingInfo", "Booking", new { BookingID = bookingIDParam })';
            },
            error: function () {
                alert("Error occured!!")
            }
        });

I get an error "The name 'bookingIDParam' does not exist in the current context

How can I call different controller/go to a url with action method with parameter from response.

Upvotes: 2

Views: 3535

Answers (1)

Mate
Mate

Reputation: 5284

Try setting some dummy string to replace it later:

var url_redirect = '@Url.Action("BookingInfo", "Booking", new { BookingID = "00replace00" })';

$.ajax({
    ....


      success: function (data) {
                url_redirect = url_redirect.replace("00replace00", data.Booking.BookingID );
                window.location.href =url_redirect ;
            },

Upvotes: 2

Related Questions