0

I'm passing a JSON object via ajax to my MVC Controller. The JSON is valid. I have a breakpoint in my MVC controller on the first line. When I inspect the variable addOnModel in the controller, the base level attributes are set correctly. The ProductCodeList attribute has the correct number of elements, but all of the values come across as null.

I have tried changing up the names of the elements to make them more unique, but that doesn't seem to help.

{"AddOnID": "0","LocaleID": "63", "Name": "FirstAddOn", "Text": "testText", "ProductCodeList": [ {"AddOnSort": 0, "AddOnProductCode": "product1"}]}

$.ajax({
contentType: 'application/json;',
dataType: 'json',
type: 'POST',
url: '/AddOn/UpsertAddOn',
data: data,
success: function () {
alert('Finished.');
}
});

/// <summary>
/// Object model from web page
//// </summary>
public class AddOnModel
{
    public int AddOnID { get; set; }
    public int LocaleID { get; set; }
    public string Text { get; set; }
    public string Name { get; set; }
    /// <summary>
    /// This is the attribute that won't populate correctly. The number
    /// of attributes in the array is correct, but all of its values
    /// come across as null. This is what I'm trying to fix.
    /// </summary>
    public List<AddOnProductModel> ProductCodeList { get; set; }
}

/// <summary>
/// Holds code and sort order. 
/// </summary>
public class AddOnProductModel
{
    int? AddOnSort { get; set; }
    string AddOnProductCode { get; set; }
}

My Controller snippet

    /// <summary>
    /// Upsert the add on.
    /// </summary>
    /// <param name="addOnModel">Model from the web page.</param>
    /// <returns></returns>
    [HttpPost]
    public ActionResult UpsertAddOn(AddOnModel addOnModel)
    {
        AddOn actualAddOn = new AddOn();
        if (addOnModel.AddOnID > 0)
        {
            //Load from DB Repository here.
        }
        //Rest of my class removed
     }

enter image description here

1
  • The properties don't match; what happens if you have all four properties in the JSON? Commented Feb 4, 2015 at 16:58

1 Answer 1

4

The properties in AddOnProductModel are not public.

Make them public

public class AddOnProductModel
{
    public int? AddOnSort { get; set; }
    public string AddOnProductCode { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Have a meeting, but will mark as answer when I get back. Thanks!

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.