我的控制器参数为空。我正在发布数据,因为我在Chrome Developer工具中可以看到它。我在MVC中有一个Person模型,该模型与下面的Person模型匹配。这是我的ViewModel:

 function Person(data) {
    this.FirstName = ko.observable(data.FirstName);
    this.LastName = ko.observable(data.LastName);
    this.Id = ko.observable(data.Id);

    this.fullName = ko.computed(function () {
        return this.FirstName() + " " + this.LastName();
    }, this);

}

function PersonViewModel() {
    var self = this;
    self.people = ko.observableArray([]);



    $.getJSON("/api/person/getpeople", function (allData) {
        var mappedTasks = $.map(allData, function (item) { return new Person(item) });
        self.people(mappedTasks);
    });


    self.save = function(){

        $.ajax("/api/person/updateperson", {
            data: ko.toJSON({ people: self.people }),
            type: "post", contentType: "application/json",
            success: function (result) { alert(result) }
        });
    }
}
ko.applyBindings(new PersonViewModel);


API控制器:

  [HttpPost]
    public bool UpdatePerson(List<Person> person)
    {
        return mgr.UpdatePerson(person);
    }

最佳答案

您需要确保服务参数的名称与您传递的名称匹配。

self.save = function(){

        $.ajax("/api/person/updateperson", {
            data: ko.toJSON({ person: self.people }),
            type: "post", contentType: "application/json",
            success: function (result) { alert(result) }
        });
    }

关于javascript - 使用Ajax将数据从 knockout 发布到Controller,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29674393/

10-09 23:34
查看更多