How to send data to the controller from the Kendo UI TreeView

不问归期 提交于 2019-12-03 20:48:23

Please try with the below code snippet.

HTML/VIEW

<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>

CONTROLLER

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!