Controller Action接收旧值作为AJAX Call上的参数

时间:2018-06-12 08:00:20

标签: javascript c# jquery asp.net asp.net-mvc

我使用简单的jquery在ASP.NET中向我的MVC Action发送Ajax请求,它第一次正常工作,但在第一次调用之后,所有调用都倾向于接收Action上的第一个发布值。 / p>

当我尝试调试我的JS代码时,新的值会在那里填充,但是在Action上,它们会变回第一个。

我的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();


                            }
                        });

                    }
                });
            }

和我的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;
        }
    }

正如您所看到的,我甚至尝试过清空缓存和清除模型状态的在线解决方案,但似乎没有解决方案。

我的AJAX调用或MVC操作有问题吗?请指教。

3 个答案:

答案 0 :(得分:1)

尝试在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();


                            }
                        });

让我们知道它是否有效

PS :在ajax调用之前,在提交处理程序中获取表单的值(Name,ContactNo,Email ...等),这可能就是它的原因总是从第一次通话中获取值。

答案 1 :(得分:0)

我不知道丢失的属性[FromBody]是否存在问题,但你可以试试看。

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

答案 2 :(得分:0)

在你的控制器中,操作后试试这个:

 ModelState.Remove("ManualBookingForm ");