简体   繁体   English

Controller Action在AJAX调用中接收旧值作为参数

[英]Controller Action receiving old values as parameters on AJAX Call

I'm sending an Ajax Request to my MVC Action in ASP.NET using simple jquery, it works fine for the first time but after the first call, all calls tend to receive the first posted values on Action. 我正在使用简单的jquery向ASP.NET中的MVC Action发送一个Ajax请求,它第一次运行良好,但是在第一次调用之后,所有调用都倾向于接收Action上发布的第一个值。

when I try to debug my JS code new values are populated there but on Action, they change back to the first ones. 当我尝试调试JS代码时,在那里会填充新值,但在Action上,它们会变回第一个。

my Jquery Code: 我的Jquery代码:

  $('.send-quote').click(function (e) {
  var $form = $('#manualBooking');

            var formData = $('#manualBooking').serializeArray();

            var name = formData[0].value;
            var contact = formData[1].value;
            var email = formData[2].value;
            var bookingDate = formData[3].value;
            var pickupLocation = formData[4].value;
            var dropOffLocation = formData[5].value;
            var flight = formData[6].value;
            var passenger = formData[7].value;
            var luggage = formData[8].value;
            var message = formData[9].value;
            var language = formData[10].value;
            var hours = formData[11].value;
            var days = formData[12].value;
            var radio = formData[13].value;


            if (radio == 'yes') {

                $form.validate({
                    errorClass: 'customErrorClass',
                    rules: {
                        name: {
                            required: true
                        },
                        phone: {
                            required: true
                        },
                        email: {
                            required: true,
                            email: true
                        },
                        depdate: {
                            required: true
                        },
                        txtAddresspickupFromAirport: {
                            required: true
                        },
                        txtAddressdropOffFromAirport: {
                            required: true
                        },
                        flight: {
                            required: true
                        },
                        passenger: {
                            required: true
                        },
                        luggage: {
                            required: true
                        },
                        message: {
                            required: true
                        },
                        lang: {
                            required: true
                        },
                        hours: {
                            required: true
                        },
                        days: {
                            required: true
                        }
                    },
                    messages: {
                        email: "*Please specify a valid email address"
                    },
                    submitHandler: function (form) {
                        console.log(' NOT OK')
                        $.ajax({
                            type: "POST",
                            url: '/Home/InsertManualBookingForm',
                            data: {
                                Name: name,
                                ContactNo: contact,
                                Email: email,
                                BookingDateTime: bookingDate,
                                PickUpLocation: pickupLocation,
                                DropOffLocation: dropOffLocation,
                                FlightNumber: flight,
                                Passenger: passenger,
                                Luggage: luggage,
                                Message: message,
                                Language: lang,
                                HoursNeeded: hours,
                                DaysNeeded: days
                            },
                            success: function (data) {
                                swal("an email will be sent to you shortly", {
                                    icon: "success",
                                });

                                //   document.getElementById("manualBooking").reset();


                            }
                        });

                    }
                });
            }

and my MVC action code: 和我的MVC操作代码:

[OutputCache(Duration = 0, VaryByParam = "none")]
    public JsonResult InsertManualBookingForm(ManualBookingForm manualBookingForm)
    {

        this.ModelState.Clear();
        ModelState.Clear(); // force to use new model values

        String Result = String.Empty;
        this.ModelState.Clear();
        ModelState.Clear(); // force to use new model values


        try
        {
            string agentCardsSerialize = JsonConvert.SerializeObject(manualBookingForm);

        this.ModelState.Clear();
            ModelState.Clear(); // force to use new model values



            Result = Common.Common.ApiCall(Common.Common.APIEndpoint + "Home/InsertManualBookingForm", agentCardsSerialize);//Common.Common.ApiCall(URL_Request, commisionType,token);
            return Json(Result, JsonRequestBehavior.AllowGet);

        }
        catch (Exception ex)
        {

            throw;
        }
    }

as you can see I even tried the online solutions of emptying the cache and clearing the Model State but no solution seems to work. 如您所见,我什至尝试了清空缓存和清除模型状态的在线解决方案,但似乎没有解决方案。

is there a problem with my AJAX call or MVC action? 我的AJAX调用或MVC操作有问题吗? please advise. 请指教。

Try Adding cache:false in your ajax call 尝试在ajax调用中添加cache:false

  $.ajax({
                            type: "POST",
                            url: '/Home/InsertManualBookingForm',
                            cache:false,
                            data: {
                                Name: name,
                                ContactNo: contact,
                                Email: email,
                                BookingDateTime: bookingDate,
                                PickUpLocation: pickupLocation,
                                DropOffLocation: dropOffLocation,
                                FlightNumber: flight,
                                Passenger: passenger,
                                Luggage: luggage,
                                Message: message,
                                Language: lang,
                                HoursNeeded: hours,
                                DaysNeeded: days
                            },
                            success: function (data) {
                                swal("an email will be sent to you shortly", {
                                    icon: "success",
                                });

                                //   document.getElementById("manualBooking").reset();


                            }
                        });

Lets us know if it works 让我们知道它是否有效

PS : Get the values of the form (Name, ContactNo, Email ... etc ) in submit handler right before ajax call, probably thats the reason it is always taking the values from the first call. PS :在ajax调用之前,立即在提交处理程序中获取表单的值(名称,ContactNo,Email ...等),这可能就是它总是从第一次调用中获取值的原因。

I don't know if the missing attribute [FromBody] is the problem but you can try and see. 我不知道是否缺少属性[FromBody]是问题,但是您可以尝试看看。

public JsonResult InsertManualBookingForm([FromBody]ManualBookingForm manualBookingForm)
{
   //Your code
}

在控制器中,操作后请尝试以下操作:

 ModelState.Remove("ManualBookingForm ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM