Data Binding in ASP.NET CORE List Box Control

22 Dec 20228 minutes to read

The ListBox loads the data either from local data sources or remote data services using the dataSource property. It supports the data type of array or DataManager.

Fields Type Description
text string Specifies the display text of each list item.
value string Specifies the hidden data value mapped to each list item that should contain a unique value.
groupBy string Specifies the category under which the list item has to be grouped.
iconCss string Specifies the iconCss class that needs to be mapped.
htmlAttributes string Allows additional attributes to configure the elements in various ways to meet the criteria.

NOTE

When binding complex data to the ListBox, fields should be mapped correctly. Otherwise, the selected item remains undefined.

Local Data

Local data can be represented by the following ways as described below.

Array of string

The ListBox has support to load array of primitive data such as strings or numbers. Here, both value and text field acts as same.

<ejs-listbox id="listbox" dataSource="@ViewBag.data" >
</ejs-listbox>
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 ListBoxController : Controller
    {
        public IActionResult databinding()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}

Array of object

The ListBox can generate its list items through an array of object data. For this, the appropriate columns should be mapped to the fields property.

In the following example, id and sports column from complex data have been mapped to the value field and text field, respectively.

<ejs-listbox id="listbox" dataSource="@ViewBag.data" >
</ejs-listbox>
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 ListBoxController : Controller
    {
        public IActionResult databinding()
        {
        List<object> Data = new List<object> {
            new { text = "Hennessey Venom", id = "list-01" },
            new { text = "Bugatti Chiron", id = "list-02" },
            new { text = "Bugatti Veyron Super Sport", id = "list-03" },
            new { text = "SSC Ultimate Aero", id = "list-04" },
            new { text = "Koenigsegg CCR", id = "list-05" },
            new { text = "McLaren F1", id = "list-06" },
            new { text = "Aston Martin One- 77", id = "list-07" },
            new { text = "Jaguar XJ220", id = "list-08" }
        ViewBag.data = Data;
        return View();
    }
}
}

Array of complex object

The ListBox can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the fields property.

In the following example, Sports.Name column from complex data have been mapped to the text field.

<ejs-listbox id="listbox" dataSource="@ViewBag.data" >
    <e-listbox-fields text="sports.Name"></e-listbox-fields>
</ejs-listbox>
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 ListBoxController : Controller
    {
        public IActionResult databinding()
        {
        List<object> Data = new List<object>
        {
            new { id = "game0", sports = new {Name = "Badminton" } },
            new { id = "game1", sports = new {Name = "Cricket" } },
            new { id = "game2", sports = new {Name = "Football" } },
            new { id = "game3", sports = new {Name = "Golf" } },
            new { id = "game4", sports = new {Name = "Tennis" } },
            new { id = "game5", sports = new {Name = "Basket Ball" } },
            new { id = "game6", sports = new {Name = "Base Ball" } },
            new { id = "game7", sports = new {Name = "Hockey" } }
        };
        ViewBag.data = Data;
        return View();
    }
}
}

Remote Data

The ListBox supports retrieval of data from remote data services with the help of DataManager component. The query property is used to fetch data from the database and bind it to the ListBox.

The following sample displays the employee names from Employee table.

<ejs-listbox id="listbox">
    <e-listbox-fields text="FirstName" value="EmployeeID"></e-listbox-fields>
    <e-data-manager url="https://services.syncfusion.com/aspnet/production/api/Employees" adaptor="ODataAdaptor" crossDomain="true"></e-data-manager>
</ejs-listbox>
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 ListBoxController : Controller
    {
        public IActionResult databinding()
        { 
            return View();
        }
    }
}