8 回答

TA贡献1921条经验 获得超9个赞
貌似JSON的格式有点问题,应该是这样写吧
{"Items" : [{"ProductId" : "4", "Qty" : "5"}, {"ProductId" : "2", "Qty" : "2"}]}

TA贡献1906条经验 获得超10个赞
JSON部分只传
[{"ProductId" : "4", "Qty" : "5"}, {"ProductId" : "2", "Qty" : "2"}]
试试

TA贡献1886条经验 获得超2个赞
传递的数据内容就是{"Items" : "[{"ProductId" : "4", "Qty" : "5"}, {"ProductId" : "2", "Qty" : "2"}]"},基本上就是这样了

TA贡献1785条经验 获得超8个赞
@浩GE: 其实不用那么麻烦的,直接传递你这个json字符串,MVC就会自动解析的。
在Basket/UpdateItems的这个方法中这样使用, public ActionResult UpdataItems(List<Product> items){}
在前台传递时,$.ajax()方法中需要把traditional设置为 true , traditional的作用和 JSON.stringify(postData)的作用是一样的。traditional,目的是为了得到:
Values=1&Values=2&Values=3;否则的话,将会得到:
Values[]=1&Values[]=2&Values[]=3
那么MVC就不认识了。

TA贡献2037条经验 获得超6个赞
坑爹的,我特意试了一下你这个代码。
发现JSON格式没有问题,后来还尝试了换一种序列化方式也没有问题,
问题在[ModelBinder(typeof(JsonModelBinder))]这句,你把2个都去掉以后试试。时间太晚了,没仔细查为什么。
我测试的Json是这个{"Items":[{"ProductId":1,"Qty":2},{"ProductId":3,"Qty":4},{"ProductId":5,"Qty":6}]}
简化了你的jQuery代码,直接提交了上面的Json。

TA贡献1877条经验 获得超6个赞
非常感谢!不过的确是出错在json格式上,因为我的页面上的一个js文件和json2.js有冲突,导致JSON.stringify函数格式化数组的时候多了2个引号,我把那个js文件去了就没问题了。
- 8 回答
- 0 关注
- 494 浏览
添加回答
举报