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