1

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?

Nikita
  • 1,019
  • 2
  • 15
  • 39
  • What is the value of your string `json` variable at the point that the exception is thrown? – darth_phoenixx Aug 13 '16 at 21:40
  • @darth_phoenixx site%5BsortedPages%5D%5B0%5D%5BsortedRows%5D=&site%5BsortedPages ...and such style to the end – Nikita Aug 13 '16 at 21:42
  • In the creation of the StreamReader you specify Encoding.UTF8 - as far as https://msdn.microsoft.com/de-de/library/system.text.encoding.utf8(v=vs.110).aspx the BOM is expected. Perhaps your ajax call posts data in a different encoding. Perhaps `new StreamReader(stream, true)` works. – Ralf Bönning Aug 13 '16 at 21:45
  • I think you should have `dataType: 'json'` in your ajax post. It's passing it as serialized form data instead by the look of it. – darth_phoenixx Aug 13 '16 at 21:46
  • @rboe It not work( I try add charset=utf-8 to contentType, and check both variants with True and Encoding.UTF8 both give the same result as was at start. Thank for comment. – Nikita Aug 13 '16 at 22:00
  • @darth_phoenixx Maybe you are right, I've tried to add to ajax: `dataType: 'json'`, it not help and adding `contentType:'application/json; charset=utf-8'` for unknow reason give error somewhere in server before my code starts so I don't know how to debug it. – Nikita Aug 13 '16 at 22:20
  • If your specifying `contentType: 'json'` then you need to stringify your data - `data: JSON.stringify({ site: getSite() })` –  Aug 13 '16 at 22:37
  • @StephenMuecke You are right, I have used this http://stackoverflow.com/questions/11606449/sending-ajax-post-to-mvc-with-application-json-charset-utf-8-returns-error-50 and it works for me. – Nikita Aug 13 '16 at 22:39

1 Answers1

0

correct ajax call

$.ajax({ 
        type: "POST",
        url: "/Site/Update",
        dataType: "json", 
        data: {JSON.stringify(filtersData) }, //without name because I'll use custom ModelBinder
        contentType: "application/json; charset=utf-8", 
        traditional: true,
        success: function (data) { 
            alert("ok"); 
        },
        error:function (xhr, ajaxOptions, thrownError) { 
            alert(xhr.status); 
            alert(thrownError); 
        }
    }); 
Nikita
  • 1,019
  • 2
  • 15
  • 39