民间, 任何人都可以帮助我或指导我一个淘汰视图模型的例子,其中包含一个传递给asp.net mvc动作的对象数组?我见过的唯一例子只显示传递的简单字符串数组。 感谢
答案 0 :(得分:4)
Here's an example from the official Knockout site。它是使用嵌套数组构建的Contacts编辑器。 [jsFiddle]
适合的ASP.NET MVC Action看起来像
public ActionResult SaveContacts(IEnumerable<Contact> contacts)
将联系人定义为班级:
public class Contact
{
public string firstName { get; set; }
public string lastName { get; set; }
public IEnumerable<Number> phones { get; set; }
}
其中Number被定义为类:
public class Number
{
public string type { get; set; }
public string number { get; set; }
}
给出example中的JavaScript Knockout View模型。您的save
方法可能如下所示
self.save = function() {
var jsonString = ko.mapping.toJSON(this.searchParams);
$.ajax({
url: "/MyController/SaveContacts",
data: jsonString,
type: 'POST',
contentType: 'application/json',
dataType: 'json'
});
};