Example of Drag and Drop in ASP.NET MVC Query Builder Control
This sample demonstrates the drag and drop support of the Query Builder control.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "dragQb",
AllowDragAndDrop = true,
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "EmployeeId", Label = "Employee ID", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "FirstName", Label = "First Name" },
new UiQueryBuilderColumn { Field = "LastName", Label = "Last Name" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "dragQb",
AllowDragAndDrop = true,
Columns = new List<UiQueryBuilderColumn>
{
new UiQueryBuilderColumn { Field = "EmployeeId", Label = "Employee ID", Type = UiQueryColumnType.Number },
new UiQueryBuilderColumn { Field = "FirstName", Label = "First Name" },
new UiQueryBuilderColumn { Field = "LastName", Label = "Last Name" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class QueryBuilderController : Controller
{
// The page is Views/QueryBuilder/DragAndDrop.cshtml; it draws what ViewData holds.
public ActionResult DragAndDrop()
{
UiRuleGroup rule = new UiRuleGroup
{
Condition = "and",
Rules =
{
new UiRule { Field = "FirstName", Operator = "startswith", Value = "A" },
new UiRule { Field = "LastName", Operator = "contains", Value = "son" },
new UiRule
{
Condition = "or",
Rules = new List<UiRule>
{
new UiRule { Field = "Country", Operator = "equal", Value = "UK" },
new UiRule { Field = "EmployeeId", Operator = "greaterthan", Value = 8 }
}
}
}
};
ViewData["rule"] = rule;
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>
In this demo, the Query Builder includes a drag-and-drop feature that allows you to move rules or groups to different positions. You can enable or disable this feature using the AllowDragAndDrop property.
In mobile mode, the control is shown vertically.