【问题标题】:Dynatree ASP.NET MVC: post tree to serverDynatree ASP.NET MVC:将树发布到服务器
【发布时间】:2013-05-28 14:43:32
【问题描述】:

我想通过 ajax 将数据从 dynatree 发布到我的 asp.net mvc 服务器。我使用来自 Steve 的模型类 (Dynatree with 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);
        }
    });

服务器:

[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":[{"标题":"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,"children":... .

【问题讨论】:

    标签: javascript asp.net serialization dynatree


    【解决方案1】:

    我想说的是:

     $.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。

    【讨论】:

    • 您的解决方案没有调用控制器函数
    • [POST("/parttree")] public ActionResult TreeData2(FormCollection form)
    • 我以前从未见过这种语法。但是,如果您使用 formcollection 并将其转换为 List 应该没问题。如果您注意到我在 ajax 调用中命名了对象树 - data: {tree: json}
    • [POST("/parttree")] 是属性路由,该函数是用我的解决方案调用的,而不是你的?
    • 或者:List ot = new JavaScriptSerializer().Deserialize>(Request.Form["tree"]);
    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2015-07-28
    • 2014-08-31
    相关资源
    最近更新 更多