JSON 配列の送信を「items」などの変数でラップする必要があります。変数名が一致することが重要です。ASP.NET は、JSON をオブジェクトに自動的に逆シリアル化します。手動で行う必要はありません。私はこれを完全にテストし、頻繁に行うので、うまくいくことがわかっています。
[HttpPost]
// Note how the argument name is "items"
public ActionResult MyItems(List<Item> items)
{
// set a breakpoint and check the items List
return Content("success")
}
public class Item
{
// Make sure to use public properties get/set
public string Category {get;set;}
}
そしてあなたのJavaScript
function Item()
{
this.Category = ko.observable();
}
function ViewModel()
{
this.Items = ko.observableArray();
this.submit = function() {
// note how we make sure argument name matches "items" as in Controller
var myData = ko.toJSON( { items: this.Items() });
$.ajax({
url: '/Home/MyItems',
contentType: 'application/json',
type: 'POST',
data: myData,
success: function(data){
// check result
}
})
}
}
var vm = new ViewModel();
ko.applyBindings(vm);
var item1 = new Item();
item1.Category("Cat1");
vm.Items.push(item1);
vm.submit();