0

我想通过 ajax 将数据从 dynatree 发布到我的 asp.net mvc 服务器。我使用来自 Steve 的模型类(带有 ASP.NET MVC 的 Dynatree),它们可以很好地从服务器获取数据到客户端。但是我在将树数据发布到服务器时仍然存在问题。

客户:

 var td = $("#tree").dynatree("getTree").toDict();
 var json = JSON.stringify(td);

 $.ajax({
        type: "POST",
        url: "/parttree",
        data: json,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        }
    });

服务器:

[POST("/parttree")]
public ActionResult TreeData2( List<TreeItem>  ot)
{
    // ot is always null here
}

VS调试器中的内容json: {"title":null,"key":"_1","isFolder":false,"isLazy":true,"tooltip":null,"href":null,"icon":null ,"addClass":null,"noLink":false,"activate":false,"focus":false,"expand":true,"select":false,"hideCheckbox":false,"unselectable":false," children":[{"title":"root","key":"_2","isFolder":false,"isLazy":false,"tooltip":null,"href":null,"icon":null ,"addClass":null,"noLink":false,"activate":false,"focus":false,"expand":false,"select":false,"hideCheckbox":false,"unselectable":false,“孩子们”:....

4

1 回答 1

0

我会说它将是以下内容:

 $.ajax({
        type: "POST",
        url: "/parttree",
        data: {tree: json},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response);
        }
    });

您的 MVC 操作将是:

    [HttpPost]
    public ActionResult PartTree(FormCollection form)
    {
        List<TreeItem> ot =  new JavaScriptSerializer().Deserialize<List<TreeItem>>(form["tree"]);

    }

尽管如果您返回 JSON,您可能正在寻找 JsonResult 而不是 ActionResult。

于 2013-05-28T15:06:15.267 回答