如何通过“可观察量”创建(“映射”)复杂类型与 Knockout.js 一起使用吗?
因此,我正在学习 knockout.js,并且我对如何在其中创建嵌套复杂类型感到有点困惑。
例如,在我的服务器端,我的模型是:
class Person {
public string Name {get; set;}
public int Age {get; set;}
public List<Colors> FavoriteColors {get; set;}
}
class Color {
public int ColorId {get; set;}
public string Name {get; set;}
}
asp.net mvc 输出的 JSON 类似于(如果我输出 List
类型):
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":{"ColorId":2,"Name":"Blue"}}]
现在我想通过映射它observables,通过 Jquery Ajax 获取。我知道 FavoriteColors
将是一个数组,但我有点困惑这里的情况如何......
任何帮助将不胜感激!
更新:
有人可以帮我解决这个问题吗? (我做了一个原型,但我的选择不起作用)
So, I'm learning knockout.js and I am a bit stumped on how to create nested complex types in it.
For example, on my server-side, my model is:
class Person {
public string Name {get; set;}
public int Age {get; set;}
public List<Colors> FavoriteColors {get; set;}
}
class Color {
public int ColorId {get; set;}
public string Name {get; set;}
}
The JSON that asp.net mvc outputs is something like (if I output a List<Person>
type):
[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":{"ColorId":2,"Name":"Blue"}}]
Now I want to map that through observables, acquired through Jquery Ajax. I know that FavoriteColors
would be an array, but I'm a bit confused how things would workout here...
Any help would be greatly appreciated!
UPDATE:
can anyone help me with this? (i made a prototype, but my select doesn't work)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 knockout 映射插件
编辑:您的示例已编辑:http://jsbin.com/eqihun/18/edit
take a look at the knockout mapping plugin
edit: your sample edited: http://jsbin.com/eqihun/18/edit
解决方案链接:此处
HTML:
link to solution: here
HTML: