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?

8
  • What is the value of your string json variable at the point that the exception is thrown? Commented Aug 13, 2016 at 21:40
  • @darth_phoenixx site%5BsortedPages%5D%5B0%5D%5BsortedRows%5D=&site%5BsortedPages ...and such style to the end Commented Aug 13, 2016 at 21:42
  • In the creation of the StreamReader you specify Encoding.UTF8 - as far as msdn.microsoft.com/de-de/library/… the BOM is expected. Perhaps your ajax call posts data in a different encoding. Perhaps new StreamReader(stream, true) works. Commented Aug 13, 2016 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. Commented Aug 13, 2016 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. Commented Aug 13, 2016 at 22:00

1 Answer 1

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); 
        }
    }); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.