【问题标题】:How to send data to the controller from the Kendo UI TreeView如何从 Kendo UI TreeView 向控制器发送数据
【发布时间】:2014-03-24 09:23:49
【问题描述】:

我有两个 TreeView,一个有国家列表,另一个是空的,现在我想将选定的国家拖放到第二个树视图中。我不知道如何从 TreeView 向控制器发送数据,并且表单中的页面上还有一些文本字段。那么,如何将表单数据和 TreeView 的数据都发送到控制器。

这是第二个空树视图的代码,我想将选定的节点添加到:

@(Html.Kendo().TreeView()
    .Name("treeview-right")
    .DragAndDrop(true)
    .Events(events => events
        .Drag("onDrag")
        .Drop("onDrop")
    )
)

【问题讨论】:

  • 使用 json ajax 调用可以将数据传递给控制器​​。如果您需要代码示例,请告诉我。
  • 是的,我想要...谢谢您的回复
  • @JayeshGoyani 谢谢你的回复,是的,我想要代码示例,请发送它,如果你有任何好的剑道 UI 树视图的博尔格和文档,那么它对我很有帮助......

标签: kendo-ui kendo-asp.net-mvc kendo-treeview


【解决方案1】:

请尝试以下代码 sn-p。

HTML/视图

<div style="border: 1px solid green;">
    <div id="treeview-left"></div>
</div>
<div style="border: 1px solid red;">
    <div id="treeview-right"></div>
</div>
<div id="mydiv" onclick="SaveData()">Click me to save data</div>
<script>
    $("#treeview-left").kendoTreeView({
        dragAndDrop: true,
        dataSource: [
            {
                id: 11, text: "Furniture", expanded: true, items: [
                  { id: 12, text: "Tables & Chairs" },
                  { id: 13, text: "Sofas" },
                  { id: 14, text: "Occasional Furniture" }
                ]
            },
            {
                id: 15, text: "Decor", items: [
                  { id: 16, text: "Bed Linen" },
                  { id: 17, text: "Curtains & Blinds" },
                  { id: 18, text: "Carpets" }
                ]
            }
        ]
    });

    $("#treeview-right").kendoTreeView({
        dragAndDrop: true,
        dataSource: [
            {
                id: 1, text: "Storage", expanded: true, items: [
                  { id: 2, text: "Wall Shelving" },
                  { id: 3, text: "Floor Shelving" },
                  { id: 4, text: "Kids Storage" }
                ]
            },
            {
                id: 5, text: "Lights", items: [
                  { id: 6, text: "Ceiling" },
                  { id: 7, text: "Table" },
                  { id: 8, text: "Floor" }
                ]
            }
        ]
    });

    var selectedID;

    function SaveData() {

        selectedID = '';

        var tv = $("#treeview-right").data("kendoTreeView");

        selectedID = getRecursiveNodeText(tv.dataSource.view());

        alert(selectedID);

        var data = {};
        data.str = selectedID;

        $.ajax({
            url: 'Home/SaveData',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function (result) {
                alert("Success");
            },
            error: function (result) {
                alert("Error");
            },
        });

    }

    function getRecursiveNodeText(nodeview) {
        for (var i = 0; i < nodeview.length; i++) {
            selectedID += nodeview[i].id + ",";
            //nodeview[i].text; You can also access text here
            if (nodeview[i].hasChildren) {
                getRecursiveNodeText(nodeview[i].children.view());
            }
        }

        return selectedID;
    }
</script>

控制器

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {

        [HttpPost]
        public JsonResult SaveData(string str)
        {
            foreach (string s in str.Split(','))
            {
                if (!string.IsNullOrEmpty(s))
                {
                    //Perform your opeartion here
                }
            }

            return Json("");
        }

    }
}

jsfiddle Demo

【讨论】:

  • 我可以通过这个 ajax 调用来发布表单数据吗?
  • 是的,你可以。为此,您必须将此 CSV 值保存在任何其他控件中并在控制器中访问它。
  • 嗨,在此示例代码中,只有默认数据意味着已提交此树视图中的野兔,但我拖放该数据未提交,为什么..?以及如何发送所有更新的数据..?
  • 我已经更新了上面的代码sn-p。现在它适用于 N 级子元素,之前它仅适用于单个子元素。
  • 对不起,但仍然存在一些问题,因为它仍然只是获取定义树数据源的默认数据,它没有获取更新的数据,即拖放数据
猜你喜欢
  • 2014-02-07
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多