0

There are a lot of question like this, but I cannot find what I'm doing wrong in my specific case. I am trying to build a JSON collection of objects and bind it to my controller action's parameter.

At the moment, I am only trying to bind Name property to the list. The only issue I can think of might be my JSON collection structure.

AJAX:

function GetFilteredDatatablesValues() {
    var data = {
        Columns: []
    };

    for (var i = 0; i < 5; i++) {
        var column = {
            Name: "name" + i
        };
        data.Columns.push(column);
    }

    data = JSON.stringify(data);

    $.ajax({
        url: '/Client/DatatablesSearchTest',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: data,
        error: function (response) {

        },
        success: function (data) {

        }
    });
}

Controller action:

public JsonResult DatatablesSearchTest(List<DataTableColumn> Columns) //Columns contains 0 items
{
    List<DataTableColumn> test = Columns;

    return Json(new { success = true });
}

Model:

public class DataTableColumn
{
    public int Data { get; set; }
    public string Name { get; set; }
    public bool Orderable { get; set; }
    public bool Searchable { get; set; }
    public Search Search { get; set; }
    public DataTableColumn()
    {
        Search = new Search();
    }
}

2 Answers 2

1

The data you send looks like this:

{
    Columns: [
        { column 1 },
        { column 2 }
    ]
}

But the data the controller expects looks like this:

[
    { column 1 },
    { column 2 }
]

I would recommend you to always create a single model that reflects exactly what you want to send to the server. So your controller action would look like this:

[HttpPost]
public JsonResult DatatablesSearchTest(SearchModel model)
{
    List<DataTableColumn> test = model.Columns;
    return Json(new { success = true });
}
public class SearchModel
{
    List<DataTableColumn> Columns { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, poke. I'm still having trouble with this. Columns remains of count 0. Would you mind providing more information, maybe how the object structure for JSON should be generated?
A count of 0? So no null but there is an actual list and that list just has no items? Then you must be doing something else, or sending an empty array to begin with.
Poke, you are right. I was initializing Columns = new List<DatatableColumn>() in the constructor. While I have things set up a bit differently now, your explanation helped me understand what was going on and I got it working. Thank you! And thank you again for the answers you provided in my past questions.
0

I confirm your code work well for .Net Framework, but not in .netcore:

The simple solution to fix the issue :

1 : Put the propertyList<DataTableColumn> Columns into RootModel:

public class RootModel
{
    public List<DataTableColumn> Columns { get; set; }
}

2 : Change the signature of the action, by adding [FromBody]RootModel rootModel like a parameter:

public JsonResult DatatablesSearchTest([FromBody]RootModel rootModel) //Columns contains 0 items
{
    //List<DataTableColumn> test = Columns;

    return Json(new { success = true });
}

Note that, without [FromBody] will gives you null object.

I hope this help you fix the issue.

4 Comments

Yes I have and thank you for the answer. But I just cannot get this to work.
@Lukas your project is .NetCore or .NetFramework? this solution work just fine for .netcore application.
It is a .NetCore project. I will continue to figure it out.
Sajid, this worked. Out of habit, I had put a constructor in the RootModel that initialized a Columns = new List<DatatableColumn>(). I removed it and it worked. Thank you Sajid!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.