I have an action method like this below.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Form newForm)
{
...
}
I have a model with the following classes, which I'd like to load the data from the ajax JSON data.
public class Form
{
public string title { get; set; }
public List<FormElement> Controls { get; set; }
}
public class FormElement
{
public string ControlType { get; set; }
public string FieldSize { get; set; }
}
public class TextBox : FormElement
{
public string DefaultValue { get; set; }
}
public class Combo : FormElement
{
public string SelectedValue { get; set; }
}
Here is the JSON data.
{ "title": "FORM1",
"Controls":
[
{ "ControlType": "TextBox", "FieldSize": "Small" ,"DefaultValue":"test"},
{ "ControlType": "Combo", "FieldSize": "Large" , "SelectedValue":"Option1" }
]
}
$.ajax({
url: '@Url.Action("Create", "Form")',
type: 'POST',
dataType: 'json',
data: newForm,
contentType: 'application/json; charset=utf-8',
success: function (data) {
var msg = data.Message;
}
});
DefaultModelBinder is handling the nested object structure but it can't resolve the different sub classes.
What would be the best way to load List with the respective sub-classes?