- ASP.NET MVC Team
- Smith
- Johnson
- Anderson
- Windows Team
- Clark
- Wright
- Lopez
Example of Drag and Drop in ASP.NET MVC TreeView Control
This ASP.NET MVC TreeView example demonstrates the drag and drop functionality of TreeView. A drag and drop image is present at the top of the sample which hides on clicking the sample. To drag and drop node, select and drag the desired node and drop it on the target node or external container.
TreeView-1
TreeView-2
- Web Team
- Joshua
- Matthew
- David
- Build Team
- Ryan
- Justin
- Robert
Drop log
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree1", AllowDragAndDrop = true, OnNodeDropped = "onDragStop", Nodes = (List<UiTreeNode>)ViewData["dragfields"] })
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree2", AllowDragAndDrop = true, OnNodeDropped = "onDragStop", Nodes = (List<UiTreeNode>)ViewData["dropfields"] })
<style>
.control-wrapper { max-width: 1000px; margin: auto; }
.control-wrapper > div { padding: 10px 15px; }
.content { border: 1px solid #dddddd; border-radius: 3px; min-height: 260px; }
.drop-log { margin: 0; padding: 8px 12px; list-style: none; font-size: 13px; }
</style>
<div class="control-section custom-tree">
<div class="control-wrapper row">
<div class="col-lg-4 tree1-data">
<h4>TreeView-1</h4>
<div class="content">
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree1", AllowDragAndDrop = true, OnNodeDropped = "onDragStop", Nodes = (List<UiTreeNode>)ViewData["dragfields"] })
</div>
</div>
<div class="col-lg-4 tree2-data">
<h4>TreeView-2</h4>
<div class="content">
@Html.UiTreeView(new UiTreeViewOptions { Id = "tree2", AllowDragAndDrop = true, OnNodeDropped = "onDragStop", Nodes = (List<UiTreeNode>)ViewData["dropfields"] })
</div>
</div>
<div class="col-lg-4 tree3-data">
<h4>Drop log</h4>
<div class="content"><ul id="dropLog" class="drop-log"></ul></div>
</div>
</div>
</div>
<script>
// Syncfusion: nodeDragStop; the move of a node, inside its tree or into the other tree, is written to the log
function onDragStop(args) {
var item = document.createElement('li');
item.textContent = args.node.text + ' dropped ' + (args.position === 'inside' ? 'into' : args.position) + ' ' + args.target.text;
var log = document.getElementById('dropLog');
log.insertBefore(item, log.firstChild);
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeViewController : Controller
{
// The page is Views/TreeView/DragandDrop.cshtml; it draws what ViewData holds.
public ActionResult DragandDrop()
{
ViewData["dragfields"] = new List<UiTreeNode>
{
new UiTreeNode { Id = "t1", Text = "ASP.NET MVC Team", Expanded = true, Children =
{
new UiTreeNode { Id = "t2", Text = "Smith" },
new UiTreeNode { Id = "t3", Text = "Johnson", Selected = true },
new UiTreeNode { Id = "t4", Text = "Anderson" }
} },
new UiTreeNode { Id = "t5", Text = "Windows Team", Expanded = true, Children =
{
new UiTreeNode { Id = "t6", Text = "Clark" },
new UiTreeNode { Id = "t7", Text = "Wright" },
new UiTreeNode { Id = "t8", Text = "Lopez" }
} }
};
ViewData["dropfields"] = new List<UiTreeNode>
{
new UiTreeNode { Id = "01", Text = "Web Team", Expanded = true, Children =
{
new UiTreeNode { Id = "02", Text = "Joshua" },
new UiTreeNode { Id = "03", Text = "Matthew" },
new UiTreeNode { Id = "04", Text = "David" }
} },
new UiTreeNode { Id = "05", Text = "Build Team", Expanded = true, Children =
{
new UiTreeNode { Id = "06", Text = "Ryan" },
new UiTreeNode { Id = "07", Text = "Justin" },
new UiTreeNode { Id = "08", Text = "Robert" }
} }
};
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The TreeView component allows users to drag any node and drop it on any other node in the same or different tree using allowDragAndDrop property. Additionally, it supports dropping a tree node to an external container using nodeDragStop event of the TreeView