In my code I have a base class Foo and all my objects inherits from the Foo object.
So let’s say I have a class like this
public class Bar : Foo {
public string Heading { get;set; }
}
I have tried to use the ApiControllers put method with dynamic but I get this error http://paste2.org/p/1914054
This is the code I’m using in the ApiController
public void Put(string id, dynamic model) {
//do stuff
}
If I use a normal controller I can use dynamic to post data. Is it possible to add make the api controller work with dynamic or do I need to build my own model binder?
It sees like some thinks that even in MVC 3 the input parameters can’t be a dynamic but that is not true and that’s why I ask this question. This controller in MVC 3 works just great with dynamic as input parameter.
I think so. Let’s take a look at the provided example:
Can you point me how/where exactly is this
editorModeldynamic variable used inside this controller action?And to even further simplify this controller action, it works, because it never never uses the dynamic variable passed as argument. I have simplified it to better illustrate what this action is roughly doing concerning model binding (throwing away of course all the infrastructure noise that we are not interested in here to illustrate the problem):
Inside this action the
TryUpdateModelandUpdateModelmethods are called on the_modelinstance variable which is passed in the constructor and is of typeIPageModel. ASP.NET MVC cannot possibly know (without a custom model binder of course) the type of your dynamic action argument. Just run this code, put a breakpoint inside theUpdateaction and observe the type of theeditorModelvariable. It will simply beSystem.Object. There are no miracles.So it’s for me it’s perfectly normal that this works the same in ASP.NET Web API.