I'm building a dynamic form creator in .net core. A "form" will consist of many different form elements. So the form model will look something like this:
public class FormModel {
public string FormName {get;set;}
public List<IElements> Elements{get;set;}
}
I have classes for TextBoxElement
, TextAreaElement
, CheckBoxElement
that all implement the IElemets
interface. And I have EditorTemplates
for each element. The code to render the form works great. Though posting the form does not work because of the List
of Interfaces.
I've been looking on how to implement a custom model binder, and seen some few examples on the web but I did not get anyone to work.
I would appreciate if someone could show me how to implement a custom model binder for this example.
Plan B: Post form as json to a web api and let JSON.Net covert it. I have tried it and it worked. In startup.cs i added:
services.AddMvc().AddJsonOptions(opts => opts.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto);
It returns type when it has to, eg. the objects in the Elements-list but not on the FormModel. But i really would like to know how to solve it with a custom model binder instead.