Created
September 13, 2011 18:28
-
-
Save jchadwick/1214598 to your computer and use it in GitHub Desktop.
JsonModelBinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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