1
所以我有一個視圖模型:麻煩與淘汰賽JSON模型解析MVC 4控制器
public class PrelimViewModel
{
public int? PrelimId { get; set; }
public int JobId { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
public string Unit { get; set; }
public int? Qty { get; set; }
public decimal? BidPrice { get; set; }
public bool Accepted { get; set; }
public int? OriginalPrelimId { get; set; }
public string Option { get; set; }
public List<RefCodeViewModel> Codes { get; set; }
public List<UnitViewModel> Units { get; set; }
public List<OptionLetterViewModel> Options { get; set; }
}
返回List<PrelimViewModel>
PrelimViewModel客戶端的列表的ko.mapper甲GetPrelim控制器的方法:
viewModel = ko.mapping.fromJS({ Prelims: data });
ko.applyBindings(viewModel);
做了一些工作,準備保存:
function savePrelims(elem) {
var $form = $(elem).parents('form');
$.ajax({
url: $form.attr('action'),
type: "POST",
data: ko.toJSON(viewModel),
datatype: "json",
contentType: "application/json charset=utf-8",
success: function(data) { toastr.success('Options Saved!'); },
error: function(data) { }
});
}
,我不能讓我的MVC方法解析JSON:
public void AddPrelims(List<PrelimViewModel> Prelims)
你已經將你的列表包裝到你的KO viewmodel中的一個屬性,所以用'data:ko.toJSON(viewModel.Prelims()),' – nemesv
這樣做吧! –
我將它包裝到一個屬性中,因爲它不知道調用foreach:在一個沒有數組父對象名稱的對象數組上,任何提示,我可能會通過data-bind =「foreach:。非常感謝! –