Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递给控制器的JSON没有值_Json_Model View Controller_Knockout.js - Fatal编程技术网

传递给控制器的JSON没有值

传递给控制器的JSON没有值,json,model-view-controller,knockout.js,Json,Model View Controller,Knockout.js,我无法将我的JSON发送到我的控制器,我也不明白为什么javascript中的值没有被传递到控制器。以下是我在javascript中的ajax帖子: this.save = function () { var data = ko.toJSON(this.Routines); $.ajax({ type: 'POST', url: "CreateJson", data: data,

我无法将我的JSON发送到我的控制器,我也不明白为什么javascript中的值没有被传递到控制器。以下是我在javascript中的ajax帖子:

this.save = function () {
        var data = ko.toJSON(this.Routines);
        $.ajax({
            type: 'POST',
            url: "CreateJson",
            data: data,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (result) {
                if (!result.success)
                    alert("test")
                else { }
            }
        })
    }
现在数据包含

[{"routine_name":"new routine","duration":"20","rest":"10","rounds":1}] 
这就是我想要的,但控制器什么也没显示。这是我的控制器

[HttpPost]
    public JsonResult CreateJson(t_routine routine, string data)
    {

        var message = "success";
        return Json(message);
    }

据我所知,MVC 3+自动接收JSON,而不需要任何参数,比如字符串数据,我只是把它扔进去试着弄清楚我是否得到了任何东西。数据为null,例程显示值为0和null。我缺少什么?

如果
t\u例程
表示

[{"routine_name":"new routine","duration":"20","rest":"10","rounds":1}]
这样就可以在
ko.toJSON
结果上调用
JSON.stringify

this.save = function () {
    var data = JSON.stringify(ko.toJSON(this.Routines));
    $.ajax({
        type: 'POST',
        url: "CreateJson",
        data: data,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (!result.success)
                alert("test")
            else { }
        }
    })
}

此时不需要控制器操作上的数据参数,您很可能需要将
t_例程
参数更改为
t_例程[]

您是否验证它是否符合正确的URL?当请求被触发时,firebug在网络中向您显示了什么?也许可以为URL尝试一个前缀/似乎我所需要的只是模型参数上的[]。stringify使参数为null,但使用默认的ko.toJSON,我在控制器中获得了所需的内容。问题解决后,我的头撞在墙上一段时间,并提示太多。非常感谢。。。