I have an action method similar below.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Form newForm) { ... }
I have a model with the following classes that I would like to load data from 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 are 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 processes the structure of nested objects, but cannot resolve various subclasses.
What would be the best way to load a List with the appropriate subclasses?
Thurein
source share