I need to use JSON NET serializer.
SiteModelBinder
internal class SiteModelBinder : System.Web.Mvc.IModelBinder
{
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
{
// use Json.NET to deserialize the incoming Position
controllerContext.HttpContext.Request.InputStream.Position = 0; // see: http://stackoverflow.com/a/3468653/331281
Stream stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
var readStream = new StreamReader(stream, Encoding.UTF8);
string json = readStream.ReadToEnd();
return JsonConvert.DeserializeObject<Site>(json, new JsonBuildBlockConverter());
}
}
Here I get an exeception unrecognized character at position 0,0
return JsonConvert.DeserializeObject<Site>(json, new JsonBuildBlockConverter());
ajax call
$.ajax({
url: '/Site/Update',
data: { site: getSite() },
contentType: 'application/json',
method: 'POST',
success: function (data) {
console.log('Success save');
},
error: function (data) {
debugBox(data);
}
});
and mvc action, BTW BuildBlocks property is of type List<AbstractBuildBlock>
and TextBlock
is derived.
public string Update(Site site)
{
//the goal to see in debugger that block is not null after the next line
TextBlock block = site.Pages[0].Rows[0].BuildBlocks[0] as TextBlock;
//siteRepository.Add(site);
return "Success";
}
What I miss or doing wrong?