Templates in ASP.NET CORE MultiSelect
1 Sep 202617 minutes to read
The MultiSelect provides several options to customize each list item, group title, selected value, header, and footer element. It uses the Essential® JS 2 Template engine to compile and render the elements properly.
Item template
The content of each list item within the MultiSelect can be customized with the itemTemplate property.
In the following sample, each list item is split into two columns to display relevant data.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)" placeholder="Select a customer" popupHeight="200px"
itemTemplate="@Html.Raw("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>")">
<e-multiselect-fields value="FirstName"></e-multiselect-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-multiselect>
</div>
</div>
<style>
.city {
right: 15px;
position: absolute;
}
</style>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult Itemtemplate()
{
return View();
}
}
}Value template
The currently selected value that is displayed by default on the MultiSelect input element can be customized using the valueTemplate property.
In the following sample, the selected value is displayed as a combined text of both FirstName and City in the MultiSelect input, which is separated by a hyphen.
<div class="control-wrapper">
<div id="default" style='padding-top:75px;'>
<ejs-multiselect id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)" placeholder="Select a customer" itemTemplate="@Html.Raw("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>")" valueTemplate="@Html.Raw("<span>${FirstName} - ${City}</span>")" popupHeight="200px">
<e-multiselect-fields text="FirstName" value="EmployeeID"></e-multiselect-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-multiselect>
</div>
</div>
<style>
.city {
right: 15px;
position: absolute;
}
</style>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult valuetemplate()
{
return View();
}
}
}Group template
The group header title under which sub-items are categorized can be customized with the groupTemplate property. This template is common for both inline and floating group header templates.
In the following sample, employees are grouped by their city.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6).where(new ej.data.Predicate('City', 'equal','london').or('City','equal','seattle'))" placeholder="Select a customer" popupHeight="200px"
groupTemplate="@Html.Raw("<strong>${City}</strong>")">
<e-multiselect-fields groupBy="City" value="FirstName"></e-multiselect-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-multiselect>
</div>
</div>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult grouptemplate()
{
return View();
}
}
}Header template
The header element is shown statically at the top of the popup list items within the MultiSelect. Any custom element can be placed as a header element using the headerTemplate property.
In the following sample, the list items and their headers are designed and displayed as two columns, similar to multiple columns of a grid.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)" placeholder="Find an Employee" popupHeight="200px"
headerTemplate="@Html.Raw("<span class='head'><span class='name'>Name</span><span class='city'>City</span></span>")"
itemTemplate="@Html.Raw("<span class='item' ><span class='name'>${FirstName}</span><span class='city'>${City}</span></span>")">
<e-multiselect-fields value="FirstName"></e-multiselect-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-multiselect>
</div>
</div>
<style>
.head, .item {
display: table;
width: 100%;
margin: auto;
}
.head {
height: 40px;
font-size: 15px;
font-weight: 600;
}
.name, .city {
display: table-cell;
vertical-align: middle;
width: 50%;
}
.head .name {
text-indent: 16px;
}
.head .city {
text-indent: 10px;
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult headertemplate()
{
return View();
}
}
}Footer template
The MultiSelect supports showing a footer element at the bottom of the list items in the popup. You can place any custom element as a footer element using the footerTemplate property.
In the following sample, the footer element displays the total number of list items present in the MultiSelect.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="employees" dataSource="@ViewBag.data" placeholder="Select a game" popupHeight="270px" mode="Box"
footerTemplate="@Html.Raw( "<span class='foot'> Total list items: " + 8 + "</span>")">
<e-multiselect-fields value="Name"></e-multiselect-fields>
</ejs-multiselect>
</div>
</div>
<style>
.foot {
text-indent: 1.2em;
display: block;
font-size: 15px;
line-height: 40px;
border-top: 1px solid #e0e0e0;
}
</style>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult footertemplate()
{
ViewBag.data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
return View();
}
}
}No records template
The MultiSelect supports customizing the popup list content when no data is found and when no matches are found on search, through the noRecordsTemplate property.
In the following sample, the popup list content displays a notification that no data is available.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="employees" dataSource="@ViewBag.data" placeholder="Select an employee" popupHeight="270px"
noRecordsTemplate="@Html.Raw("<span class='norecord'> NO DATA AVAILABLE</span>")">
<e-multiselect-fields value="Name"></e-multiselect-fields>
</ejs-multiselect>
</div>
</div>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult norecords()
{
ViewBag.data = new string[] { };
return View();
}
}
}Action failure template
You can also customize the popup list content when the data fetch request fails at the remote server. This can be done with the actionFailureTemplate property.
In the following sample, when the data fetch request fails, the MultiSelect displays a notification.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-multiselect id="customers" query="new ej.data.Query().from('Employees').select(['FirstName']).take(6)" placeholder="Select a customer" popupHeight="200px"
actionFailureTemplate="@Html.Raw("<span class='action-failure'> Data fetch get fails</span>")">
<e-multiselect-fields value="FirstName"></e-multiselect-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svcs/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-multiselect>
</div>
</div>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult actionfailure()
{
return View();
}
}
}Summary Tag Template
The Summary Tag Template feature displays selected items as a formatted summary text in the input field instead of listing all selections individually. This is especially useful in CheckBox mode when working with large datasets and using the SelectAll option, as it significantly improves performance.
The summaryTagCount property sets a threshold—when the number of selected items exceeds this threshold, the summaryTagTemplate displays a custom formatted text instead of individual items.
Template Placeholder Properties
The following properties are available for use in the summary template placeholders:
| Placeholder | Description |
|---|---|
${selectedCount} |
Total count of currently selected items |
${totalCount} |
Total number of items in the data source |
In the following sample, the Summary Tag Template displays the count of selected items when the threshold is exceeded.
<div class="control-wrapper">
<div id="default" style='padding-top:75px;'>
<ejs-multiselect id="records" dataSource="@ViewBag.data" placeholder="Select a item" value="@ViewBag.value" mode="CheckBox" showDropDownIcon="true" showSelectAll="true" allowFiltering="true" enableVirtualization="true" popupHeight="200px" maximumSelectionLength="15000" summaryTagCount="5" summaryTagTemplate="${selectedCount} items selected">
<e-multiselect-fields value="Text" text="Text"></e-multiselect-fields>
</ejs-multiselect>
</div>
</div>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class MultiSelectController : Controller
{
public ActionResult summarytagtemplate()
{
ViewBag.data = new Record().RecordModelList();
ViewBag.value = new Record().RecordModelList().Select(e => e.Text).ToArray();
return View();
}
}
}```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public class Record
{
public string ID { get; set; }
public string Text { get; set; }
public List<Record> RecordList { set; get; }
public List<Record> RecordModelList()
{
return Enumerable.Range(1, 15000).Select(i => new Record()
{
ID = "id" + i,
Text = "Item " + i,
}).ToList();
}
}
}
```Limitation: The Summary Tag Template feature only works in CheckBox mode because it has a built-in SelectAll option for bulk selection, which triggers the need for performance optimization. Other modes (Default, Box, Delimiter) don’t support SelectAll and use different display formats, making template-based formatting unnecessary.
Note: When you set a threshold value and preselected items exceed it, the summary template displays formatted text instead of individual items. If
summaryTagTemplateandsummaryTagCountare not provided, the Summary Tag Template feature is automatically enabled when records or preselected items exceed 1000, displaying a default summary format for better performance.