以下是我从控制器返回的内容:
var states = _dc.States.ToList();
var serializer = new JavaScriptSerializer();
ViewBag.StateList = serializer.Serialize(states);
我的JS:
var stateList = @Html.Raw(ViewBag.StateList);
var stateListModel = ko.mapping.fromJS(stateList);
我是console.log
stateListModel
,它总是[]
,但stateList
是包含所有数据的完整数组。
答案 0 :(得分:0)
ko.mapping结果不是空数组,它是一个函数。
正如nemesv的评论所说,你必须这样做:
var stateListModel = ko.mapping.fromJS(stateList);
var realArray = stateListModel();