Example of Drag And Drop in ASP.NET MVC ListBox Control
This sample demonstrates the drag and drop functionalities of a ListBox. Select and drag an item and drop it within the same list box or into another list box.
Group A
- Australia
- Bermuda
- Canada
- Cameroon
- Denmark
- France
- Finland
- Germany
- Hong Kong
Group B
- India
- Italy
- Japan
- Mexico
- Norway
- Poland
- Switzerland
- United Kingdom
- United States
@Html.UiListBox("listbox1", new UiListBoxOptions
{
Scope = "listbox2",
AllowDragAndDrop = true,
Height = "330px",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DataSource = (IEnumerable<object>)ViewData["groupA"]
})
@Html.UiListBox("listbox2", new UiListBoxOptions
{
Scope = "listbox1",
AllowDragAndDrop = true,
Height = "330px",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DataSource = (IEnumerable<object>)ViewData["groupB"]
})
@Html.UiButton(new UiButtonOptions { Id = "savechange", Text = "Update" })
<style>
.dual-list-wrapper { display: flex; justify-content: center; gap: 48px; padding-top: 30px; }
.dual-list-box { width: 300px; }
</style>
<div class="control-section">
<div class="dual-list-wrapper">
<div class="dual-list-box">
<h4>Group A</h4>
@Html.UiListBox("listbox1", new UiListBoxOptions
{
Scope = "listbox2",
AllowDragAndDrop = true,
Height = "330px",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DataSource = (IEnumerable<object>)ViewData["groupA"]
})
</div>
<div class="dual-list-box">
<h4>Group B</h4>
@Html.UiListBox("listbox2", new UiListBoxOptions
{
Scope = "listbox1",
AllowDragAndDrop = true,
Height = "330px",
Fields = new UiDropDownFields { Text = "Name", Value = "Code" },
DataSource = (IEnumerable<object>)ViewData["groupB"]
})
</div>
</div>
<div style="text-align:center; margin-top: 16px;">
@Html.UiButton(new UiButtonOptions { Id = "savechange", Text = "Update" })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ListBoxController : Controller
{
// The page is Views/ListBox/DragAndDrop.cshtml; it draws what ViewData holds.
public ActionResult DragAndDrop()
{
List<object> groupA = new List<object>();
groupA.Add(new { Name = "Australia", Code = "AU" });
groupA.Add(new { Name = "Bermuda", Code = "BM" });
groupA.Add(new { Name = "Canada", Code = "CA" });
groupA.Add(new { Name = "Cameroon", Code = "CM" });
groupA.Add(new { Name = "Denmark", Code = "DK" });
groupA.Add(new { Name = "France", Code = "FR" });
groupA.Add(new { Name = "Finland", Code = "FI" });
groupA.Add(new { Name = "Germany", Code = "DE" });
groupA.Add(new { Name = "Hong Kong", Code = "HK" });
ViewData["groupA"] = groupA.ToArray();
List<object> groupB = new List<object>();
groupB.Add(new { Name = "India", Code = "IN" });
groupB.Add(new { Name = "Italy", Code = "IT" });
groupB.Add(new { Name = "Japan", Code = "JP" });
groupB.Add(new { Name = "Mexico", Code = "MX" });
groupB.Add(new { Name = "Norway", Code = "NO" });
groupB.Add(new { Name = "Poland", Code = "PL" });
groupB.Add(new { Name = "Switzerland", Code = "CH" });
groupB.Add(new { Name = "United Kingdom", Code = "GB" });
groupB.Add(new { Name = "United States", Code = "US" });
ViewData["groupB"] = groupB.ToArray();
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 ListBox component allows the user to drag and drop a desired item from one list box into another list box. The drag and drop feature can be enabled by using the following properties,
- To drag and drop a desired item within the ListBox, the
AllowDragAndDropproperty should be set totrue. - To drag and drop between two listboxes, the
Scopeproperty should be set to both the listboxes.
In this sample, a list of countries is loaded in Group A and another list of countries is loaded in Group B. You can drag and drop an item or multiple items from Group A to Group B, and vice versa. By clicking update button, user can save the changes to the corresponding JSON using Datamanager saveChanges method.