2

have tried the methods found but still cannot seem to tackle the issue. am passing a list of object via Jquery Aja but it seems that the controller is receiving null values (it is recognizing the list count fine).

the jquery code is as follows:

var xmlids = Array();

        for (var i = 0; i < orderId.length; i++) {
            var xdata = {
                "XmlOid": "" +orderId[i] + "", 
                "CourierCompany": $("#CourierCompany_"+orderId[i]).val() ,
                "CourierService": $("#CourierService_" + orderId[i]).val(),
                "StoreId" :  storeId 
            };
            xmlids.push(xdata);
        }

var data = { invoices: xmlids };


        $.ajax({
            url: purl,
            type: "POST",
            dataType: "json",
            //contentType: "application/json;",
            async: false,

            data: data,

The controller param is :

public JsonResult ProcessSaleOrder(List<XMLInvoiceGeneration> invoices)

the object is as follows:

public class XMLInvoiceGeneration
    {
        public Int64 XmlOid { get; set; }
        public string CourierCompany { get; set; }
        public string CourierService { get; set; }
        public Int64? StoreId { get; set; }
    }

any help appreciated.

4
  • Can you check with DevTools (click F12 in browser and turn on Network monitoring) what exactly is posted via ajax call? Commented Oct 16, 2014 at 19:40
  • Is there a reason you have quotes around the XmlOid in your javascript? Try removing that and then running it. Commented Oct 16, 2014 at 19:46
  • had logged the array i console and it has all values set properly. @Gjohn added that just to make that int a string. however issue is same with or without them Commented Oct 16, 2014 at 20:07
  • Try to stringify the data and send it as @JohnTrack said. Commented Oct 17, 2014 at 6:46

4 Answers 4

3

I tried to replicate your problem.

I created a new ASP.NET MVC web application, and in the Home controller's Index view, I added the following

<input type="button" id="send" value="Send" />
@section scripts{
    <script type="text/javascript">
        $(function () {
            $("input#send").on("click", function () {
                var xmlids = Array();

                for (var i = 0; i < 2; i++) {
                    var xdata = {
                        "XmlOid": 1,
                        "CourierCompany": "TempData",
                        "CourierService": "Temp",
                        "StoreId": 2
                    };

                    xmlids.push(xdata);
                }

                var data = { invoices: xmlids };

                $.ajax({
                    url: "/Home/ProcessSaleOrder",
                    type: "POST",
                    dataType: "json",
                    async: false,
                    data: data
                });
            });
        })
    </script>
}

I basically created some temp data, but trying to still use your original idea. My controller looks like this:

[HttpPost]
public ActionResult ProcessSaleOrder(List<XMLInvoiceGeneration> invoices)
{
    return new EmptyResult();
}

When I click the button, and hit the breakpoint in the controller, the invoices parameter contains 2 items, both with the values I set in the JavaScript code. I'm not sure why your code isn't working, but maybe you can use mine as a template to help you narrow down what the issue is.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this uncomment //contentType: "application/json;",

And send data with JSON.stringify(data)

data: JSON.stringify(data)

PS: Best practice for url : url: "@Url.Action("method","controller")"

1 Comment

thank you for the answer. the answer seemed to work 80% . the following worked : contentType: "application/json; charset=utf-8;" and then strinfying the data
1

I had the same issue on sending a List to the controller. After a lot of effort, I found the reason why it is sending as null. It's the fields with Return Type(Class) that are not added with {get; set; }

 $.ajax({
            url: "/Home/CreateTasks",
            data: ko.toJSON({Id: this.Id, tasks: objList }),             
            type: "POST",
            dataType: "json",
            processData: true,
            contentType: "application/json; charset=utf-8",
            success: data => {
}

And my Model Class is: After

public class TaskFields
{
    public string Title { get; set; }
    public string Activity { get; set; }
}
public class CreateTaskInput
{
    public int Id { get; set; }
    public TaskFields TaskProperties { get; set; }
}

Before it was:

public class TaskFields
{
    public string Title;
    public string Activity;
}
public class CreateTaskInput
{
    public int Id;
    public TaskFields TaskProperties;
}

And My Controller Method is:

[HttpPost]
public JsonResult MethodName(int Id, List<CreateTaskInput> tasks)
{
}

I am not sure this answer will help you. But it may help someone who has a similar mistake. Thanks!

Comments

0

Add [FromBody] to the parameters in action

[HttpPost]
public ActionResult ProcessSaleOrder([FromBody] List<XMLInvoiceGeneration> invoices)
{
    return new EmptyResult();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.