Skip to content

Instantly share code, notes, and snippets.

@jonhilt
Created May 26, 2010 13:25
Show Gist options
  • Save jonhilt/414467 to your computer and use it in GitHub Desktop.
Save jonhilt/414467 to your computer and use it in GitHub Desktop.
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (!IsJSONRequest(controllerContext))
{
return base.BindModel(controllerContext, bindingContext);
}
// get the JSON data that's been posted
var request = controllerContext.HttpContext.Request;
var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();
return new JavaScriptSerializer()
.Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
}
static bool IsJSONRequest(ControllerContext controllerContext)
{
var contentType = controllerContext.HttpContext.Request.ContentType;
return contentType.Contains("application/json");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment