Example of Default Functionalities in ASP.NET MVC Query Builder Control
This sample demonstrates the Query Builder control showing how to import and export a query in JSON format. The query preview can be showcased in the tab control, and the query can be imported to Query Builder using the Import button.
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "defaultQb",
EnableNotCondition = true,
OnCreated = "QbDefault.show",
OnRuleChange = "QbDefault.show",
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 = "Title", Label = "Title" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } },
new UiQueryBuilderColumn { Field = "HireDate", Label = "Hire Date", Type = UiQueryColumnType.Date },
new UiQueryBuilderColumn { Field = "Salary", Label = "Salary", Type = UiQueryColumnType.Number, Step = 1000 },
new UiQueryBuilderColumn { Field = "IsActive", Label = "Active", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
@Html.UiQueryBuilder(new UiQueryBuilderOptions
{
Id = "defaultQb",
EnableNotCondition = true,
OnCreated = "QbDefault.show",
OnRuleChange = "QbDefault.show",
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 = "Title", Label = "Title" },
new UiQueryBuilderColumn { Field = "Country", Label = "Country", Values = new List<object> { "USA", "UK", "Ireland" } },
new UiQueryBuilderColumn { Field = "HireDate", Label = "Hire Date", Type = UiQueryColumnType.Date },
new UiQueryBuilderColumn { Field = "Salary", Label = "Salary", Type = UiQueryColumnType.Number, Step = 1000 },
new UiQueryBuilderColumn { Field = "IsActive", Label = "Active", Type = UiQueryColumnType.Boolean }
},
Rule = (UiRuleGroup)ViewData["rule"]
})
<pre id="defaultRules" class="small mt-3 p-2 border rounded" aria-live="polite" aria-label="The rule set"></pre>
<script>
window.QbDefault = (function () {
'use strict';
/** OnCreated and OnRuleChange: the rule set as text (without the ids the Query Builder gives its rules). */
function show() {
var rules = window.Lumen.get('defaultQb').getRules();
document.getElementById('defaultRules').textContent = JSON.stringify(rules, function (key, value) { return key === 'id' ? undefined : value; }, 2);
}
return { show: show };
})();
</script>
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/Default.cshtml; it draws what ViewData holds.
public ActionResult Default()
{
UiRuleGroup rule = new UiRuleGroup
{
Condition = "and",
Rules =
{
new UiRule { Field = "Country", Operator = "equal", Value = "USA" },
new UiRule { Field = "Salary", Operator = "greaterthanorequal", Value = 50000 }
}
};
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>
The Query Builder control is used to create or edit filters. You can edit filters by changing the fields. In this demo features export and import of queries in JSON format is showcased. For Common Expression Language (CEL) output, use the "cel" format. CEL is used for validating data. For Spring Expression Language (SpEL) output, use the "spel" format. The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime.
In mobile mode, the control is shown vertically.