将参数从ajax成功响应传递到Url Action方法

时间:2019-05-21 22:23:16

标签: c# jquery ajax asp.net-mvc razor

我对Api进行了ajax调用,将数据添加到数据库中,我希望成功函数调用带有操作方法的控制器,该控制器与从API调用的响应接收到的参数不同。我该怎么办

Ajax呼叫

 $.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!!")
            }
        });

我收到错误消息“当前上下文中不存在名称'bookingIDParam'

如何使用响应方法中带有参数的操作方法调用不同的控制器/转到URL。

1 个答案:

答案 0 :(得分:1)

尝试设置一些虚拟字符串以在以后替换它:

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 ;
            },