ASP.Net MVC : Sending JSON to Controller ASP.Net MVC : Sending JSON to Controller json json

ASP.Net MVC : Sending JSON to Controller


I use a custom model binder for json like this:

public class JsonModelBinder<T> : IModelBinder {    private string key;    public JsonModelBinder(string requestKey) {        this.key = requestKey;    }    public object BindModel(ControllerContext controllerContext, ...) {        var json = controllerContext.HttpContext.Request[key];        return new JsonSerializer().Deserialize<T>(json);    }}

And then wire it up in Global.asax.cs like this:

ModelBinders.Binders.Add(    typeof(Product),    new JsonModelBinder<Product>("ProductJson"));

You can read more about this here: Inheritance is Evil: The Epic Fail of the DataAnnotationsModelBinder

EDIT

The JsonModelBinder should be used on the controller action parameter typed as Product only. The Int32 and ClassObject should fall back to the DefaultModelBinder. Are you experiencing a different result?