值不来自于razor ajax到action方法

时间:2022-08-25 14:03:14

Id in the action method is always null. lost where i am doing anything wrong. action method is called as expected.

操作方法中的Id总是null。迷失在我做错的地方。按预期调用action方法。

jQuery function:

jQuery功能:

function success(result) {
    var Id = $('#UserId').val();
    var data = JSON.stringify({ 'Id': Id });
    alert(data);
    $.ajax({
        type: "GET",
        url: "@Url.Action("ListAppointments", "Appointment")",
        data: data,

        success: function (result2) {
            $("#partialViewAppointments").html(result2);
            $('#example').DataTable();

        }
    });
}

Action Method:

操作方法:

public PartialViewResult ListAppointments(string Id)
{
    var userId = Convert.ToInt32(Id);
    var o = (from s in db.tblAppointments.ToList()
             where s.UserId == userId
             select new AppointmentViewModel { AppointmentInstructorName = s.InstructorName, AppointmentLessonAddress = s.Address, LessonDateTime = s.LessonDate, UserId = s.UserId, Id = s.ID });

    return PartialView(o);
}

3 个解决方案

#1


2  

You do not need to do JSON stringify on your data. You can send the js object as it is.

不需要对数据执行JSON stringify。可以按原样发送js对象。

var d ={ Id:  $('#UserId').val()};
$.ajax({
            type: "GET",
            url: "@Url.Action("ListAppointments", "Home")",
            data: d
            success: function (result2) {
              $("#partialViewAppointments").html(result2);
              $('#example').DataTable();
            }
   });

Now since this is a GET request the data (the js object) will be send as a querystring values to the server (Ex : ListAppointments?Id=23)

由于这是一个GET请求,数据(js对象)将作为一个querystring值发送到服务器(例如:listappointment ?Id=23)

So when you do JSON.stringify call on that js object, It will return a string like "{"Id":23}". So your final url used for the ajax call(with the querystring) will be ListAppointments?{"Id":23}. You can see that this is not valid. It should be ListAppointments?Id=23

JSON的时候。对该js对象进行stringify调用,它将返回一个字符串,如“{"Id":23}"。因此,用于ajax调用(使用querystring)的最终url将是listappointment ?{“Id”:23}。你可以看到这是无效的。它应该ListAppointments ? Id = 23

If you still want to use JSON.stringify (to send complex data), specify contentType and use POST method.

如果您还想使用JSON的话。stringify(用于发送复杂数据),指定内容类型并使用POST方法。

Also i see you are converting the string param value to int in your action method, why not use int as parameter type since you are sending numeric data ?

我还看到您正在将字符串param值转换为int类型,为什么不使用int作为参数类型,因为您正在发送数字数据?

public PartialViewResult ListAppointments(int Id)
{

}

#2


0  

by doing this way, i have it working. not sure why the stringify function would stop it from working:

通过这种方式,我让它工作。不知道为什么stringify函数会停止工作:

  function success(result) {
        var Id = $('#UserId').val();
        var data = ({ Id: Id });
        alert(data);
        $.ajax({
            type: "GET",
            url: "@Url.Action("ListAppointments", "Appointment")",
            data: data,
            success: function (result2) {
                $("#partialViewAppointments").html(result2);
                $('#example').DataTable();

            }
        });
    }

#3


0  

Your missing Content-Type:'application/json' but assuming you have left the default route you can just pass it with the query parameter

您丢失的内容类型:“应用程序/json”,但是假设您已经离开了默认的路径,您可以将它与查询参数一起传递。

function success(result) {
    var Id = $('#UserId').val();
    alert(data);
    $.ajax({
        type: "GET",
        url: "@Url.Action("ListAppointments", "Appointment")" + "?id=" + Id,
        data: data,
        success: function (result2) {
            $("#partialViewAppointments").html(result2);
            $('#example').DataTable();

        }
    });
}

#1


2  

You do not need to do JSON stringify on your data. You can send the js object as it is.

不需要对数据执行JSON stringify。可以按原样发送js对象。

var d ={ Id:  $('#UserId').val()};
$.ajax({
            type: "GET",
            url: "@Url.Action("ListAppointments", "Home")",
            data: d
            success: function (result2) {
              $("#partialViewAppointments").html(result2);
              $('#example').DataTable();
            }
   });

Now since this is a GET request the data (the js object) will be send as a querystring values to the server (Ex : ListAppointments?Id=23)

由于这是一个GET请求,数据(js对象)将作为一个querystring值发送到服务器(例如:listappointment ?Id=23)

So when you do JSON.stringify call on that js object, It will return a string like "{"Id":23}". So your final url used for the ajax call(with the querystring) will be ListAppointments?{"Id":23}. You can see that this is not valid. It should be ListAppointments?Id=23

JSON的时候。对该js对象进行stringify调用,它将返回一个字符串,如“{"Id":23}"。因此,用于ajax调用(使用querystring)的最终url将是listappointment ?{“Id”:23}。你可以看到这是无效的。它应该ListAppointments ? Id = 23

If you still want to use JSON.stringify (to send complex data), specify contentType and use POST method.

如果您还想使用JSON的话。stringify(用于发送复杂数据),指定内容类型并使用POST方法。

Also i see you are converting the string param value to int in your action method, why not use int as parameter type since you are sending numeric data ?

我还看到您正在将字符串param值转换为int类型,为什么不使用int作为参数类型,因为您正在发送数字数据?

public PartialViewResult ListAppointments(int Id)
{

}

#2


0  

by doing this way, i have it working. not sure why the stringify function would stop it from working:

通过这种方式,我让它工作。不知道为什么stringify函数会停止工作:

  function success(result) {
        var Id = $('#UserId').val();
        var data = ({ Id: Id });
        alert(data);
        $.ajax({
            type: "GET",
            url: "@Url.Action("ListAppointments", "Appointment")",
            data: data,
            success: function (result2) {
                $("#partialViewAppointments").html(result2);
                $('#example').DataTable();

            }
        });
    }

#3


0  

Your missing Content-Type:'application/json' but assuming you have left the default route you can just pass it with the query parameter

您丢失的内容类型:“应用程序/json”,但是假设您已经离开了默认的路径,您可以将它与查询参数一起传递。

function success(result) {
    var Id = $('#UserId').val();
    alert(data);
    $.ajax({
        type: "GET",
        url: "@Url.Action("ListAppointments", "Appointment")" + "?id=" + Id,
        data: data,
        success: function (result2) {
            $("#partialViewAppointments").html(result2);
            $('#example').DataTable();

        }
    });
}