我想使用淘汰賽的視圖上傳文件和顯示列表。爲此,我使用jquery.form.js以便使用ajax上傳它們。我已經改變了使用淘汰賽,我的視圖模型看起來像這樣通過AJAX上傳文件時更新淘汰賽viewmodel
var ViewModel = function (groups) {
var self = this;
self.groups = ko.observableArray(ko.utils.arrayMap(groups, function (group) {
return {
planName: ko.observable(group.Key),
documentList: ko.observableArray(ko.utils.arrayMap(group.Values, function (value) {
return {
document: ko.observable(new Document(value))
};
}))
};
}));
var options = {
dataType: 'json',
success: submissionSuccess
};
self.add = function() {
$('#addForm').ajaxSubmit(options);
return false;
};
function submissionSuccess(result) {
alert('success');
}
};
有一個Document功能來做映射。從控制器接收Json數據時,我卡住了。結果是正確的,這是我在第一次加載時收到的格式相同的對象列表,但我不知道如何「刷新」視圖模型以使用此新列表。
不知道使用ko映射插件會使它更容易,因爲我從來沒有使用它,甚至不知道它是否適用於此。
控制器法中,是相關的,是這樣的(如果其他neede讓我知道althoug將無法訪問的代碼在接下來的幾個小時)
[HttpPost]
public ActionResult AddDocument(AddDocumentViewModel viewModel)
{
var partyId = Utils.GetSessionPartyId();
if (viewModel.File.ContentLength > Utils.GetKBMaxFileSize * 1024)
ModelState.AddModelError("File", String.Format("The file exceeds the limit of {0} KB", Utils.GetKBMaxFileSize));
if (ModelState.IsValid)
{
_documentsManager.AddDocument(viewModel, partyId);
if (Request.IsAjaxRequest())
{
var vm = _displayBuilder.Build(partyId);
return Json(vm.Documents);
}
return RedirectToAction("Index");
}
var newViewModel = _createBuilder.Rebuild(viewModel, partyId);
return PartialView("_AddDocument", newViewModel);
}
感謝
編輯我想出了這個代碼,似乎工作(該功能視圖模型裏面一個
function submissionSuccess(result) {
self.groups(ko.utils.arrayMap(result, function (group) {
return {
planName: ko.observable(group.Key),
documentList: ko.utils.arrayMap(group.Values, function (value) {
return {
document: new Document(value)
};
})
};
}));
};
所以基本上你需要更換可觀察組的內容? –
是的。理想情況下,我想替換一個(或者在不存在的情況下添加它),但它可以替代所有這些文件 – mitomed