Getting Started with ASP.NET Core Query Builder Control

27 Jul 202613 minutes to read

This section briefly explains how to include the ASP.NET Core Query Builder control in your ASP.NET Core application using Visual Studio, and Visual Studio Code.

Create an ASP.NET Core Web App with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Run the following command to create a new ASP.NET Core Web App.

dotnet new webapp -o RazorPagesMovie
code -r RazorPagesMovie

Alternatively, create an ASP.NET Core Web App using Visual Studio Code via Microsoft Templates, or the Syncfusion® ASP.NET Core Extension, or the C# Dev Kit extension.

Install the required ASP.NET Core packages

Install the Syncfusion.AspNetCore.QueryBuilder and Syncfusion.AspNetCore.Themes NuGet packages. All Syncfusion ASP.NET Core packages are available on nuget.org. See the NuGet packages topic for the details.

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Syncfusion.AspNetCore.QueryBuilder and Syncfusion.AspNetCore.Themes) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.AspNetCore.QueryBuilder -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Open the terminal and run the following commands.

dotnet add package Syncfusion.AspNetCore.QueryBuilder -v 34.1.29
dotnet add package Syncfusion.AspNetCore.Themes -v 34.1.29

Add ASP.NET Core Tag Helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.QueryBuilder Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.QueryBuilder

Add stylesheet and script resources

The theme stylesheet and script can be referenced from CDN. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml

<head>
    ...
    @* ASP.NET Core controls styles *@
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    @* ASP.NET Core controls scripts *@
    <script src="_content/Syncfusion.AspNetCore.QueryBuilder/scripts/sf-query-builder.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    @*  ASP.NET Core Script Manager *@
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core Query Builder control

Add the ASP.NET Core Query Builder control in the ~/Pages/Index.cshtml file.

@page
@model IndexModel

@{
    List<string> values = new List<string> { "Mr.", "Mrs." };
}

<ejs-querybuilder id="querybuilder" width="73%">
	<e-querybuilder-columns>
		<e-querybuilder-column field="EmployeeID" label="Employee ID" type="number"></e-querybuilder-column>
		<e-querybuilder-column field="FirstName" label="First Name" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="TitleOfCourtesy" label="Title Of Courtesy" type="boolean" values="values"></e-querybuilder-column>
		<e-querybuilder-column field="Title" label="Title" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="HireDate" label="Hire Date" type="date" format="dd/MM/yyyy"></e-querybuilder-column>
		<e-querybuilder-column field="Country" label="Country" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="City" label="City" type="string"></e-querybuilder-column>
	</e-querybuilder-columns>
 </ejs-querybuilder>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core Query Builder control will render in your default web browser.

Open the terminal and run the following command.

dotnet run

ASP.NET Core Query Builder Control

Render with rule

The following example shows a basic ASP.NET Core Query Builder control with a rule.

@page
@model IndexModel
@using Syncfusion.EJ2.QueryBuilder

@{
	List<string> values = new List<string> { "Mr.", "Mrs." };
    QueryBuilderRule rule = new QueryBuilderRule()
    {
        Condition = "and",
        Rules = new List<QueryBuilderRule>()
        {
            new QueryBuilderRule { Label="Employee ID", Field="EmployeeID", Type="number", Operator="equal", Value = 1 },
            new QueryBuilderRule { Label="Title", Field="Title", Type="string", Operator="equal", Value = "Sales Manager" }
        }
    };
    var dataSource = EmployeeView.GetAllRecords();
}

<ejs-querybuilder id="querybuilder" width="73%" rule="rule" dataSource="dataSource">
	<e-querybuilder-columns>
		<e-querybuilder-column field="EmployeeID" label="Employee ID" type="number"></e-querybuilder-column>
		<e-querybuilder-column field="FirstName" label="First Name" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="TitleOfCourtesy" label="Title Of Courtesy" type="boolean" values="values"></e-querybuilder-column>
		<e-querybuilder-column field="Title" label="Title" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="HireDate" label="Hire Date" type="date" format="dd/MM/yyyy"></e-querybuilder-column>
		<e-querybuilder-column field="Country" label="Country" type="string"></e-querybuilder-column>
		<e-querybuilder-column field="City" label="City" type="string"></e-querybuilder-column>
	</e-querybuilder-columns>
 </ejs-querybuilder>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPagesMovie.Pages;

public class IndexModel : PageModel
{
    public void OnGet()
    {

    }
}
public class EmployeeView
{
    public EmployeeView()
    {

    }
    public EmployeeView(int EmployeeID, string FirstName, string LastName, string Title, DateTime BirthDate, DateTime HireDate, int ReportsTo, string Address, string PostalCode, string Phone, string City, string Country)
    {
        this.EmployeeID = EmployeeID;
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Title = Title;
        this.BirthDate = BirthDate;
        this.HireDate = HireDate;
        this.ReportsTo = ReportsTo;
        this.Address = Address;
        this.PostalCode = PostalCode;
        this.Phone = Phone;
        this.City = City;
        this.Country = Country;

    }
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Title { get; set; }
    public DateTime BirthDate { get; set; }
    public DateTime HireDate { get; set; }

    public int ReportsTo { get; set; }

    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public static List<EmployeeView> GetAllRecords()
    {
        List<EmployeeView> Emp = new List<EmployeeView>();
        Emp.Add(new EmployeeView(1, "Nancy", "Davolio", "Sales Representative", new DateTime(1948, 12, 08), new DateTime(1992, 05, 01), 2, "507 - 20th Ave. E.Apt. 2A ", " 98122", "(206) 555-9857 ", "Seattle ", "USA"));
        Emp.Add(new EmployeeView(2, "Andrew", "Fuller", "Vice President, Sales", new DateTime(1952, 02, 19), new DateTime(1992, 08, 14), 4, "908 W. Capital Way", "98401 ", "(206) 555-9482 ", "Kirkland ", "USA"));
        Emp.Add(new EmployeeView(3, "Janet", "Leverling", "Sales Representative", new DateTime(1963, 08, 30), new DateTime(1992, 04, 01), 3, " 4110 Old Redmond Rd.", "98052 ", "(206) 555-8122", "Redmond ", "USA "));
        Emp.Add(new EmployeeView(4, "Margaret", "Peacock", "Sales Representative", new DateTime(1937, 09, 19), new DateTime(1993, 05, 03), 6, "14 Garrett Hill ", "SW1 8JR ", "(71) 555-4848 ", "London ", "UK "));
        Emp.Add(new EmployeeView(5, "Steven", "Buchanan", "Sales Manager", new DateTime(1955, 03, 04), new DateTime(1993, 10, 17), 8, "Coventry HouseMiner Rd. ", "EC2 7JR ", " (206) 555-8122", "Tacoma ", " USA"));
        Emp.Add(new EmployeeView(6, "Michael", "Suyama", "Sales Representative", new DateTime(1963, 07, 02), new DateTime(1993, 10, 17), 2, " 7 Houndstooth Rd.", " WG2 7LT", "(71) 555-4444 ", "London ", "UK "));
        Emp.Add(new EmployeeView(7, "Robert", "King", "Sales Representative", new DateTime(1960, 05, 29), new DateTime(1994, 01, 02), 7, "Edgeham HollowWinchester Way ", "RG1 9SP ", "(71) 555-5598 ", "London ", " UK"));
        Emp.Add(new EmployeeView(8, "Laura", "Callahan", "Inside Sales Coordinator", new DateTime(1958, 01, 09), new DateTime(1994, 03, 05), 9, "722 Moss Bay Blvd. ", "98033 ", " (206) 555-3412", "Seattle ", "USA "));
        Emp.Add(new EmployeeView(9, "Anne", "Dodsworth", "Sales Representative", new DateTime(1966, 01, 27), new DateTime(1994, 11, 15), 5, "4726 - 11th Ave. N.E. ", "98105 ", "(71) 555-5598 ", " London", "UK "));
        return Emp;
    }
}

ASP.NET Core Query Builder with Rule

NOTE

View Sample in GitHub.

See also

  1. Getting Started with ASP.NET Core using Razor Pages
  2. Getting Started with ASP.NET Core MVC using Tag Helper