简体   繁体   English

如何将我返回的Ajax数据作为C#对象列表传递给控制器​​?

[英]how to pass my returning ajax data to a controller as a list of c# objects?

I have written an ajax function that would receive a json result from the controller. 我编写了一个ajax函数,该函数将从控制器接收json结果。 Now, I want that in every call I pass the returned data to the controller and cast it ac# object. 现在,我希望在每次调用中都将返回的数据传递给控制器​​,并将其转换为ac#对象。

I am using jquery1.7 and I tried this: 我正在使用jquery1.7,我试过了:

$("document").ready(function () {
    var returningData="";
    $("select").change(function () {
        $.getJSON("/product/GetProductDetailsByValue", { amount: $(this).val(),returningProductDetails:returningData }, function (data) {
        returningData = data.serialize();
        });
    });
});

And in controller I have this: 在控制器中,我有这个:

[HttpGet]
public JsonResult GetProductDetailsByValue(string amount, string returningProductDetails)
{
    List<ProductDetail> obj = (new JavaScriptSerializer()).Deserialize<List<ProductDetail>>(returningProductDetails);
...
}

But the returningProductDetails is always null. 但是returningProductDetails始终为null。

I tried to send data directly without serializing it too and in ontroller I tried to receive List<ProductDetail> but I received a list in controller that has some objects but all of their fields were null. 我尝试直接发送数据而不进行序列化,在ontroller中,我尝试接收List<ProductDetail>但是我在控制器中收到一个包含一些对象但所有字段均为空的列表。

How to do this? 这个怎么做?

Update 更新

When I send the data directly without serializing I will receive some objects with null fields but in my browser's console I can see that the params contains an amount and some ProductDetails and the productdetails are not null at all while when I set a breakpoint in the controller I see some null productdetails. 当我直接发送数据而不进行序列化时,我会收到一些带有空字段的对象,但是在浏览器的控制台中,我看到在控制器中设置断点时,参数包含一个数量,并且一些ProductDetails和productdetails都不为空。我看到一些null产品详细信息。 I think in the controller I am not waiting for a proper data type. 我认为在控制器中,我不是在等待适当的数据类型。

try changing your returing project details in your javascript to this 尝试将您的JavaScript中的重现项目详细信息更改为此

 $("select").change(function () {
$.ajax({
                    type: "GET",
                    url: "/product/GetProductDetailsByValue",
                    data: { amount: $(this).val(), returningProductDetails: returningData },
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                    returningData = data;

                    }
                });

});) });)

and use this in your controller (as you have previously) 并在控制器中使用它(如前所述)

List<ProductDetail> returningProductDetails

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

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