ASP MVC 5如何读取请求对象使用JSON NET的帮助 [英] ASP MVC 5 How to read object from request with the help of JSON NET

查看:129
本文介绍了ASP MVC 5如何读取请求对象使用JSON NET的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JSON NET序列化。



SiteModelBinder

 内部类SiteModelBinder:System.Web.Mvc.IModelBinder 
{
公共对象BindModel(controllerContext controllerContext,System.Web.Mvc.ModelBindingContext的BindingContext)
{
//使用JSON .NET反序列化传入的位置
controllerContext.HttpContext.Request.InputStream.Position = 0; //看到:http://stackoverflow.com/a/3468653/331281
流流= controllerContext.RequestContext.HttpContext.Request.InputStream;
变种readStream =新的StreamReader(流Encoding.UTF8);
JSON字符串= readStream.ReadToEnd();
返回JsonConvert.DeserializeObject&所述;网站>(JSON,新JsonBuildBlockConverter());
}
}

下面我在位置0获得exeception无法识别的字符, 0

 返回JsonConvert.DeserializeObject<&网站GT;(JSON,新JsonBuildBlockConverter()); 



Ajax调用

 < 。code> $阿贾克斯({
网址:'/网站/更新',
数据:{网站:getSite()},
的contentType:应用/ JSON',
法:POST,
成功:功能(数据){
的console.log('成功保存');
},
错误:功能(数据) {
debugBox(数据);
}
});

和MVC行动,BTW BuildBlocks物业类型列表与LT; AbstractBuildBlock> 的TextBlock 是的。

 公共字符串更新(网站网站)
{
//在调试器块不是下一行
TextBlock的块之后空看目标= site.Pages [0] .Rows [0]。 BuildBlocks [0]作为TextBlock的;
//siteRepository.Add(site);
返回成功;
}



我错过了什么或做错了?


< DIV CLASS =h2_lin>解决方案

正确的Ajax调用

  $。阿贾克斯({
型:POST,
URL:/网站/更新,
数据类型:JSON,
数据:{JSON.stringify(filtersData)} //没有名字因为我将使用自定义的模型绑定器
的contentType:应用/ JSON的;字符集= UTF-8,
传统:真的,
成功:功能(数据){
警报( OK);
},
错误:功能(XHR,ajaxOptions,thrownError){
警报(xhr.status);
警报(thrownError);
}
});


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?

解决方案

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); 
        }
    }); 

这篇关于ASP MVC 5如何读取请求对象使用JSON NET的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆