作为参数发送到控制器函数的observableArray为null

时间:2013-03-19 19:05:31

标签: asp.net-mvc knockout.js

我想将FeatureList(observableArray)中的数据传递给TreeController中的SaveMappings函数。我已将一个按钮绑定到sendItems函数。我在var data = p2fData行放了一个断点来看看我收到了什么。 p2fData为空。

我将控制器更改为public JsonResult SaveMappings(List p2fData)。在这种情况下,p2f数据显示有1个元素,但是它也是null。

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);
    }
}

1 个答案:

答案 0 :(得分:0)