【发布时间】:2014-06-25 12:27:53
【问题描述】:
所以我有一个 ViewModel:
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> 的 GetPrelim 控制器方法
PrelimViewModel 客户端列表的 ko.mapper:
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 视图模型中的一个属性中,因此与
data: ko.toJSON(viewModel.Prelims()),非常吻合 -
我将它包装到一个属性中,因为它不知道调用 foreach: 在没有父名称的数组对象数组上,任何提示,我虽然可能 data-bind="前锋:。”非常感谢!
标签: c# asp.net-mvc json asp.net-mvc-4 knockout.js