为什么它的值不发送到 ajax post?

Why it value does not send in to ajax post?

我是 ajax 的新手。我在将数据发送到 ajax post 时遇到问题。 console.log(obj.Id)console.log(oke)的输出是2。然后我尝试通过ajax中的数据发送它,但它在控制器中最终为0。

$(function () {
            $("body").on('click', '#btnEdit', function () {
                alert("clicked ok");
                $("#addRowModal").modal("hide");
                var obj = {};
                obj.Id = $(this).attr('data-id');
                oke = $(this).data("id");
                console.log(obj.Id)
                console.log(oke)
 
                $.ajax({
                    url: '@Url.Action("Details", "InvoicePPh")',
                    data: oke,
                    type: 'POST',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        alert("sukses");
                    },
                    error: function(response) { 
                        alert("error") 
                    }
                });
            });
        });

我的控制器看起来像这样

[HttpPost]
        public JsonResult Details(int id)
        {
            var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
            InvoicePPh pph = new InvoicePPh();
            pph2326.TaxForm = obj.TaxForm;              
            return Json(pph);
        }

我想要传递到我的控制器的“2”值,我该怎么做?谢谢你的帮助。

请更改ajax部分的数据属性。

          $.ajax({
                url: '@Url.Action("Details", "InvoicePPh")',
                data: { 'id': oke },
                type: 'POST',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert("sukses");
                },
                error: function(response) { 
                    alert("error") 
                }
            });

使用 Ajax 将数据发送到 Controller 方法的另一种方法是将数据包装在 JSON 对象中,然后将其发送到服务器进行处理。然后服务器将反序列化您的 JSON 对象,您可以从该进程访问所需的属性:

$(function () {
    $("body").on('click', '#btnEdit', function () {
        alert("clicked ok");
        $("#addRowModal").modal("hide");
        var obj = {};
        obj.Id = $(this).attr('data-id');
        oke = $(this).data("id");
        console.log(obj.Id)
        console.log(oke)

        var json = {
           oke: oke
        };
        
        $.ajax({
            url: '@Url.Action("Details", "InvoicePPh")',
            data: {'json': JSON.stringify(json)},
            type: 'POST',
            dataType: "json",
            success: function (response) {
                alert("sukses");
            },
            error: function(response) { 
                alert("error") 
            }
        });
    });
});

您的 Controller 方法将是:

using System.Web.Script.Serialization;

[HttpPost]
public JsonResult Details(string json)
{
    var serializer = new JavaScriptSerializer();
    dynamic jsondata = serializer.Deserialize(json, typeof(object));

    //Get your variables here from AJAX call
    var id= Convert.Int32(jsondata["id"]);
    var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
    InvoicePPh pph = new InvoicePPh();
    pph2326.TaxForm = obj.TaxForm;              
    return Json(pph);
}

如果您只需要方法参数中的 id,只需将 ajax 中的数据更改为:

 contentType: "application/x-www-form-urlencoded",
 data: { 'id': oke },

id 是来自控制器方法的参数名称。