服务器向客户端发送一个复杂对象的位置,目标是从 C# 的“foreach”转换到 KnockoutJS 的“data-bind="foreach:”,请考虑使用各种信息填充购物车的这段代码:
@{
foreach (var item in GetItems(Model))
{
<dt>
<input type="radio" id='mode_@(item.ID)' name="mode" value="@item.ID" />
@item.Label - $@item.PriceToAdd
</dt>
<dd>
@Html.Raw(item.Explanation) </dd>
}
}
}
是否应该在渲染视图之前调整服务器的代码以展平对象,或者 KnockoutJS 是否可以处理展开它?如果服务器发送 JSON 会更容易吗?
跟进: 很明显,问题归结为映射插件,mfanto 的第一个答案让我成功了:
self.items = ko.mapping.fromJS(@Html.Raw(JsonConvert.SerializeObject(Model.Items)));
firebug 向我显示以下输出:
self.items = ko.mapping.fromJS([{"ID":60},{"ID":62},{"ID":63},{"ID":64},{"ID":9}]);
也许映射器失败是因为我的一个项目(id = 9)与其他项目具有不同的元素。
可能我需要研究映射器的一种更先进的用法?
格式化输出比较 JsonConvert 与 JavaScriptSerializer 返回的值
...
self.itemsJSON = ko.mapping.fromJS(@Html.Raw(JsonConvert.SerializeObject(Model.Items)));
self.items = @Html.Raw(new JavaScriptSerializer().Serialize(Model.Items));
当上面的代码渲染到 Firebug 中的断点时:
self.itemsJSON = ko.mapping.fromJS([{"ID":60},{"ID":62},{"ID":63},{"ID":64},{"ID":9}]);
self.items = [ //line breaks inserted for clarity
{"Explanation":"Item1's text.","Label":"Item1's Label","MsgConfirm":null,"PriceToAdd":1255,"TaxExempt":false,"PercentToAdd":0,"SortOrder":1,"ID":60},
{"Explanation":"Item2's text.","Label":"Item2's Label","MsgConfirm":null,"PriceToAdd":1255,"TaxExempt":false,"PercentToAdd":0,"SortOrder":2,"ID":62},
{"Explanation":"Item3's text.","Label":"Item3's Label","MsgConfirm":null,"PriceToAdd":295,"TaxExempt":false,"PercentToAdd":0,"SortOrder":3,"ID":63},
{"Explanation":"Item4's text.","Label":"Item4's Label","MsgConfirm":null,"PriceToAdd":395,"TaxExempt":false,"PercentToAdd":0,"SortOrder":4,"ID":64},
{"Explanation":null,"Label":"[foo]","MsgConfirm":null,"PriceToAdd":150,"TaxExempt":false,"PercentToAdd":0,"SortOrder":99,"ID":9}
];
谢谢