0

This is my first time using JSON and I'm having a bit of trouble parsing and using it in my controller. I already tried these solutions Unable to successfully receive JSON data to .NET MVC Controller, How to receive JSON as an MVC 5 action method parameter, but none of them made it for me.

I'm retrieving some data from my html and sending the arrays through ajax to the controller in the following code:

JQuery:

var values = $('ul#values li').map(function() {
   return this.textContent;
}).get()

var lines = $('ul#lines li').map(function() {
    return {
       Column: this.textContent,
       Table: this.getAttribute("name")
    };
}).get();

var columns = $('ul#lines li').map(function() {
    return {
       Column: this.textContent,
       Table: this.getAttribute("name")
    };
}).get();

var filters = $('ul#filters li.panel').map(function () {

            var n = this.getAttribute("name");

            var c = $(this).children()[1].textContent;

            var r = $(this).find("li input:checked").map(function() {
                return this.parentNode.textContent;
            }).get();
            if (r.length === 0)
                return null;
            else
                return { table: n, name:c, vals: r };
        }).get();


 $.ajax({
        type: "POST",
        url: "/Home/PivotTable",
        dataType: "json",
        traditional: true,
        data: {
            indicators: values,
            lines: JSON.stringify(lines) ,
            columns:  JSON.stringify(columns),
            filters:  JSON.stringify(filters)
        },
        success: function (data) {
            alert(data.s, data.l);
        }

    });

And this is the form data being posted:

indicators:  Taxa Mensal de inflação
lines:[{"Column":"  Base","Table":"D_Base"}]
columns:[{"col":"  Trimestre","table":"D_Calendario"}]
filters:[{"table":"D_PF_ClasseProdutos","name":"  Código","vals":[" 22"," 23"]}]

And receiving the data in the controller where I need to access the data:

public JsonResult PivotTable(string[] indicators, Lines[]  lines, object[] columns, object[] filters)
        {
            string sa = indicators[0];

            var l = lines;
            var col = columns;
            var fil = filters;

            var result = new { s = sa, l=l[0].Column};
            return Json(result, JsonRequestBehavior.AllowGet);
        }

public class Lines
        {
            public string Column { get; set; }
            public string Table { get; set; }
        }

I tried binding lines to an object, like the solution in one of the links, but got

NullRefrenceException was unhandled by usercode

While the other parameters receive the appropriate data, but i can´t access the properties of the object.

What am I doing wrong, and how can i parse the data or bind it to an object?

zriv
  • 19
  • 1
  • 4
  • Please show us the code for the `Lines` class. Also, you can't use `object[] columns` like that (same for filters). You need to have `YourConcreteClass[] columns` instead. – mjwills Jul 10 '17 at 12:20
  • 1
    Remove the `JSON.stringify()` calls. You're effectively double-wrapping the JSON in those properties as jQuery will serialise the entire object before sending – Rory McCrossan Jul 10 '17 at 12:22
  • The code for Lines class is bellow the controller, but using a class gave me the NullReference error. In the meantime I will try what you sugested – zriv Jul 10 '17 at 12:25
  • Removing the `JSON.stringify()` made the data an object instead of JSON – zriv Jul 10 '17 at 14:14

1 Answers1

-2

You can try to use Json.NET library which allows you to serialize and deserialize.

Pass your params as string and do the following:

Lines[] m = JsonConvert.DeserializeObject<Lines>(lines); 
Dovchik
  • 410
  • 5
  • 9
  • Your solution solve my problem. I had to change `Lines[ ] m = JsonConvert.DeserializeObject(lines);` Thanks! – zriv Jul 10 '17 at 14:11