2013-05-28 93 views
0

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

客户:

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

服务器:的json在VS调试

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

内容: { “标题”:空, “关键”: “_ 1”, “isFolder”:假的“isLazy”:真正的 “提示”:空, “HREF”:空, “图标”:空, “addClass”:空, “noLink”:假的, “激活”:假的, “焦点访谈”:假”扩大 “:真正的” 选择 “:假的,” hideCheckbox “:假的,” 不可选择 “:假的,” 孩子 “:[{” 称号 “:” 根”, “关键”: “_ 2”, “isFolder”:假的“isLazy”:假的, “提示”:空, “HREF”:空, “图标”:空” addClass “:空,” noLink “:假的,” 激活 “:假的,” 焦点访谈 “:假的,” 扩展 “:假的,” 选择 “:假的,” hideCheckbox “:假的,” 不可选择 “:假的,” 孩子” :....

回答

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"]); 

    } 

虽然你可能寻找到一个JsonResult不是ActionResult如果你正在返回JSON。

+0

您的解决方案不调用控制器功能 – daniel

+0

[POST(“/ parttree”)] public ActionResult TreeData2(FormCollection form) – Steve

+0

我从来没有见过这种语法。但是,如果您使用formcollection并将其转换为列表,那应该没问题。如果您注意到我已经在ajax调用中命名了对象树 - data:{tree:json} – Steve

相关问题