模型绑定POST与NancyFx中的对象数组 [英] Model-bind POST with Array of objects in NancyFx
问题描述
我有一个具有以下结构的json对象
I have a json object with the following structure
{
ContainerType: "Box",
Items : [{Name: "Coin", Amount: 5}, {Name: "Spyglas", Amount : 1}]
}
该对象在Nancy后端系统中有一个表示:
This object has an representation in the Nancy backend system:
public string ContainerType {get; set}
public IEnumberable<Item> Items {get; set}
其中Item(如预期)是具有公共属性的非常简单的对象:
where Item (as expected) is a very simple object with public properties:
public string Name {get; set;}
public double Amount {get; set;}
我的问题是,当我收到我的模块(基于NancyModule) string属性被绑定在这行代码中:
My problem is that when I receive the Post in my module (based on NancyModule) only the string property is being binded in this line of code:
var model = this.databind<MyContainerModel>();
我已经尝试更改列表类型以更具体(List< MyContainerModel>),到Arrays MyContainerModel []),但似乎没有任何工作。
I've tried with changing list types to more specific (List<MyContainerModel>), to Arrays (MyContainerModel[]), but nothing seems to work.
Nancy的git-repo的演示部分中的数据绑定示例仅处理int的反序列化,但是我的问题是使用javascript对象反序列化数组。
The data-binding examples in the demo section of the git-repo for Nancy only deals with deserialization of ints, but my question is for the deserialization of arrays with javascript objects.
任何想法?
谢谢! >
Thanks!
推荐答案
几个小时后,我找到了解决方案。问题不是南希的一部分,那是我的ajax的帖子没有正确的标题。添加 contentType 和 dataType 的技巧:
After a few hours I found the solution. The problem was not the Nancy part of it, it was that my ajax post did not have the right headers. adding contentType and dataType did the trick:
$.ajax({
url: '/add',
type: 'POST',
data: normalModel,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
})
希望这有助于未来读者!
Hope this helps future readers!
这篇关于模型绑定POST与NancyFx中的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!