在初始页面加载时,我的数据加载正常。我有一个删除函数,它通过 .ajax 调用我的控制器并删除数据库中的项目 OK,然后发回新的 JSON 项目列表。一切正常,直到我调用 ko.mapping.fromJS(data.newData, viewModel) 然后页面停止,没有错误消息。如果我添加一个警报(data.newData),我会看到正确的 Json。以下是代码,谁能看到我做错了什么?
<script>
var initialData = @Html.Raw(Json.Encode(Model));
var viewModel = {
trainingDocs : ko.observableArray(initialData)
};
function deleteDoc(doc) {
$.ajax({
url: "/Admin/_DeleteTrainingDocument/"+doc.TrainingDocumentId,
type: "POST",
data: "",
success: function (data) {
if (data.Result) {
$("#userMessage").html("<img src='/content/Status_1.png' align='bottom' />" + data.Message);
ko.mapping.fromJS(data.newData, viewModel);
} else {
$("#userMessage").html("<img src='/content/Status_0.png' align='bottom' />" + data.Message);
}
},
error: function (jqXhr, textStatus, errorThrown) {
alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')");
},
complete: function () {
}
});
};
ko.applyBindings(viewModel, document.body);
</script>
<table class="agent-info">
<thead>
<tr>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody data-bind="foreach: trainingDocs">
<tr>
<td><span data-bind="text:Title"></span></td>
<td><a href="#" id="whiteLinks" data-bind="click: function() { if(confirm('Are you sure you want to delete '+$data.Title+'?')) deleteDoc($data) }">delete</a></td>
</tr>
</tbody>
</table>
<div id="userMessage">
Results
</div>