简体   繁体   中英

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. Now, I want that in every call I pass the returned data to the controller and cast it ac# object.

I am using jquery1.7 and I tried this:

$("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.

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.

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. 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

 $("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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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