0
我想將FeatureList(observableArray)中的數據傳遞給TreeController中的SaveMappings函數。我已將一個按鈕綁定到sendItems函數。我在var data = p2fData行放置了一個斷點來查看我收到的內容。 p2fData爲空。observableArray作爲參數發送到控制器函數爲空
我將控制器更改爲public JsonResult SaveMappings(List p2fData)。在這種情況下,p2f數據顯示有1個元素,但它的空值也是。
var Feature = function (featId) {
var self = this;
self.featId = featId;
self.parameters = ko.observableArray();
}
var ParameterToFeatureListViewModel = function() {
var self = this;
var newFeature = new Feature("Feature XYZ");
newFeature.parameters.push("1");
newFeature.parameters.push("2");
self.FeatureList = ko.observableArray([newFeature]);
self.sendItems = function() {
$.ajax({
type: "POST",
url: '/Tree/SaveMappings',
data: ko.toJSON(self.FeatureList),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (request, status, error) {
alert(request.statusText);
}
});
}
}
var vm = new ParameterToFeatureListViewModel()
ko.applyBindings(vm);
public class TreeController:Controller
{
public ActionResult Index(){...}
[HttpPost]
public JsonResult SaveMappings(string p2fData)
{
var data = p2fData;
return Json(data);
}
}
你沒有,你有SaveMappings作爲參數一些真正的C#類型?爲什麼你想以字符串的形式獲取所有數據爲JSON?但是如果你想讓JSON嘗試'data:JSON.stringify({p2fData:ko.toJSON(self.FeatureList)}),' – nemesv 2013-03-19 19:44:48
我做了JSON.stringify之後,我確實從FeaturedList中獲取了數據。我認爲ko.toJSON做了同樣的事情。 – shresthaal 2013-03-19 20:24:37