Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Created September 13, 2011 18:28
Show Gist options
  • Save jchadwick/1214598 to your computer and use it in GitHub Desktop.
Save jchadwick/1214598 to your computer and use it in GitHub Desktop.
JsonModelBinder
using System.Web.Mvc;
using Newtonsoft.Json;
public class JsonModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var providerResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var json = providerResult.AttemptedValue;
if (json == null)
return null;
return JsonConvert.DeserializeObject(json, bindingContext.ModelType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment