0

Some objects in JSON are not binding to Model.

JS

var filterSet = {
    "Filters":[Object1],
    "FilterSets":[{
        "Filters":[Object2,Object3],
        "FilterSets":[{
            "Filters":[Object4,Object5],
            "FilterSets":[]
        }]
    }]
}
$.ajax({
    url: '/ControllerName/GetData',
    dataType: 'json',
    data: JSON.stringify({
        filterSet: filterSet
    }),
    type: 'POST',
    contentType: 'application/json; charset=utf-8'
}).done(function (result) {
    ...
});

Controller

public JsonResult GetData(FilterSet filterSet)
{
    ....

    return Json(data);
}

Model

public class Filter
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    ...

    public Filter()
    {
        ...
    }
}

public class FilterSet
{
    public List<Filter> Filters { get; set; }
    public List<FilterSet> FilterSets { get; set; }

    public FilterSet()
    {
        Filters = new List<Filter>();
        FilterSets = new List<FilterSet>();
    }
}

The Filters list with Object4 and Object5 is bound, but the Filters list with Object1 and the Filters list with Object2 and Object3 are not. Or perhaps they are but are not getting populated with the correct values.

So all Filters siblings of non-empty FilterSets are just empty lists.

It doesn't seem to be the JS object causing this weird issue. I think it's something with the Model. What's missing from the Model? Or what's wrong in general?

1

1 Answer 1

1

Renaming the fields solved the issue. Perhaps, the pluralized field name was too close to the class name.

public class FilterSet
{
    public List<Filter> FilterList { get; set; }
    public List<FilterSet> FilterSetList { get; set; }

    public FilterSet()
    {
        FilterList = new List<Filter>();
        FilterSetList = new List<FilterSet>();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Racking my brains as one bind wasn't working, gave me an idea and renaming one field fixed it....crazy. +1

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.