Correct Model Binding for Derived Types in ASP.NET MVC 6
I ran into another fun problem today (why do so many of my blog posts contain that phrase?): I'm working on an ASP.NET 5 (MVC 6 / DNX) application, and I'd created a class that derived from another class. For example: (pseudo-code) public class Element public class HeaderElement : Element There's also a class which contains a generic list of the parent type: (pseudo-code) public class Section { ... (other stuff) ... public List Elements { get; set; } } Nothing unusual here, right? Well, I was sending that Section class shown above to client-side calls in JSON format, and everything was fine. However, when POSTing or PUTing back to my server-side controller, all instances of that derived HeaderElement class were instead deserialized as instances of plain old Element (the base class). It looked like I was going to have to write my own custom model binder for this... ...or so I thought. Turns out, there was a much simpler solution: setting the TypeNam