为什么从jquery传递给控制器​​方法的参数为null,即使它不是?

时间:2022-03-13 20:35:21

I have been trying to call my .cs method in the controller from jquery json and it gets called but the parameter passed is always null. Why? Even i checked in the console log and it shows the value being passed but somehow it doesn't get passed to the method. It's null.

我一直试图从jquery json中调用控制器中的.cs方法并调用它,但传递的参数始终为null。为什么?即使我检查了控制台日志,它显示了传递的值,但不知何故它没有传递给方法。它是空的。

 $('#AppointmentDate').change(function () {

                var AppointmentDate = '2018-04-30'; //document.getElementById('AppointmentDate').value;

                $.ajax
                ({
                    url: '@Url.Action("GetTimeSlotsByDate", "Appointment")',
                    type: 'GET',
                    contentType: "application/json; charset= utf-8",
                    data: JSON.stringify(AppointmentDate),
                    dataType: 'json',
                    success: function (results) {

                        $("#fk_TimeSlotID").html(""); // clear before appending new list 

                        $.each(results, function (i, slot) {
                            $("#fk_TimeSlotID").append(
                                $('<option></option>').val(slot.TimeSlotID).html(slot.FromTo));
                        });

                        console.log('Time slots returned');
                        console.log(AppointmentDate);

                    }
                });

Method:

 public ActionResult GetTimeSlotsByDate(DateTime? RequestedAppointmentDate)
        {
            TimeSlotsRepository TimeSlotsRep = new TimeSlotsRepository();
            List<TimeSlotsModel> ListTimeSlotsModel = TimeSlotsRepository.getTimeSlotsByDate(RequestedAppointmentDate);

            return Json(ListTimeSlotsModel, JsonRequestBehavior.AllowGet);
        }

This is the rendered URL

这是呈现的URL

http://localhost:13924/Appointment/GetTimeSlotsByDate?"2018-04-30"

2 个解决方案

#1


0  

You can convert your data to query string parameters and pass to server in url as

您可以将数据转换为查询字符串参数,并将其传递给url中的服务器

somesite.com/Appointment/GetTimeSlotsByDate?RequestedAppointmentDate=your date

#2


0  

try this ,Its working for me

试试这个,它为我工作

 $('#AppointmentDate').change(function () {

           // var AppointmentDate = '2018-04-30'; //document.getElementById('AppointmentDate').value;

            $.ajax
            ({
                url: '@Url.Action("GetTimeSlotsByDate", "Appointment")',
                type: 'GET',
                contentType: "application/json; charset= utf-8",
                //data: JSON.stringify(AppointmentDate),
                data: { RequestedAppointmentDate: "2018-04-30" },
                dataType: 'json',
                success: function (results) {

                    $("#fk_TimeSlotID").html(""); // clear before appending new list 

                    $.each(results, function (i, slot) {
                        $("#fk_TimeSlotID").append(
                            $('<option></option>').val(slot.TimeSlotID).html(slot.FromTo));
                    });

                    console.log('Time slots returned');
                    console.log(AppointmentDate);

                }
            });

#1


0  

You can convert your data to query string parameters and pass to server in url as

您可以将数据转换为查询字符串参数,并将其传递给url中的服务器

somesite.com/Appointment/GetTimeSlotsByDate?RequestedAppointmentDate=your date

#2


0  

try this ,Its working for me

试试这个,它为我工作

 $('#AppointmentDate').change(function () {

           // var AppointmentDate = '2018-04-30'; //document.getElementById('AppointmentDate').value;

            $.ajax
            ({
                url: '@Url.Action("GetTimeSlotsByDate", "Appointment")',
                type: 'GET',
                contentType: "application/json; charset= utf-8",
                //data: JSON.stringify(AppointmentDate),
                data: { RequestedAppointmentDate: "2018-04-30" },
                dataType: 'json',
                success: function (results) {

                    $("#fk_TimeSlotID").html(""); // clear before appending new list 

                    $.each(results, function (i, slot) {
                        $("#fk_TimeSlotID").append(
                            $('<option></option>').val(slot.TimeSlotID).html(slot.FromTo));
                    });

                    console.log('Time slots returned');
                    console.log(AppointmentDate);

                }
            });