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
}
