0
我有一個MVC控制器的以下功能如何通過knockoutjs視圖模型爲MVC控制器
public class XyzController:Controller
{
public ActionResult Index(){...}
[HttpPost]
public bool Save(string jsondata)
{
//parse json data to insert into the database
}
}
我想這個視圖模型傳遞到保存功能
var myObj = function (id) {
var self = this;
self.myId = id;
self.parameters = ko.observableArray();
}
var ViewModel = function() {
var self = this;
self.myObjList = ko.observableArray();
self.sendItems = function() {
$.ajax({
type: "POST",
url: '/Xyz/Save',
data: ko.toJSON(self),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (request, status, error) {
alert(request.statusText);
}
});
}
}
var vm = new ViewModel()
ko.applyBindings(vm);
我做得到如果我將數據作爲JSON.stringify({jsondata:ko.toJSON(self)})傳遞數據,但是如何將其轉換爲對象以便我可以迭代myObjList和參數?
只要有操作方法接受任何的對象類型的對象。讓模型聯編程序完成這項工作。 – 2013-03-20 15:25:49
完全同意Andrew的觀點:你繞開了MVC的天生能力之一。更改簽名以接收目標對象類型,並讓MVC爲您執行映射。 – 2013-03-21 13:53:56