Search results

Data Binding

The MultiSelect 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.

The MultiSelect also supports different kinds of data services such as OData, OData V4, and Web API, and data formats such as XML, JSON, and JSONP with the help of DataManager adaptors.

Fields Type Description
text string Specifies the display text of each list item.
value number or 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 icon class of each list item.

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

Binding local data

Local data can be represented in two ways as described below.

1. Array of string

The MultiSelect has support to load array of primitive data such as strings and numbers. Here, both value and text field act the same.

tagHelper
arrayofstrings.cs
<div>
    <ejs-multiselect id="games" dataSource="@ViewBag.data" placeholder="Favorite Sports" mode="box">
        <e-multiselect-fields value="id" text="sports"></e-multiselect-fields>
    </ejs-multiselect>
</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 IActionResult arrayofstrings()
        {
            ViewBag.data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
            return View();
        }
    }
}

2. Array of object

The MultiSelect 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, Id column and Game column from complex data have been mapped to the value field and text field, respectively.

tagHelper
arrayofobjects.cs
GameList.cs
<div>
    <ejs-multiselect id="games" dataSource="@ViewBag.data" placeholder="Favorite Sports" mode="box">
        <e-multiselect-fields value="Id" text="Game"></e-multiselect-fields>
    </ejs-multiselect>
</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 IActionResult arrayofobjects()
        {
            ViewBag.data = new GameList().GameLists();
            return View();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class GameList
    {
        public string Id { get; set; }
        public string Game { get; set; }

        public List<GameList> GameLists()
        {
            List<GameList> game = new List<GameList>();
            game.Add(new GameList { Id = "Game1", Game = "American Football" });
            game.Add(new GameList { Id = "Game2", Game = "Badminton" });
            game.Add(new GameList { Id = "Game3", Game = "Basketball" });
            game.Add(new GameList { Id = "Game3", Game = "Basketball" });
            game.Add(new GameList { Id = "Game4", Game = "Cricket" });
            game.Add(new GameList { Id = "Game5", Game = "Football" });
            game.Add(new GameList { Id = "Game6", Game = "Golf" });
            game.Add(new GameList { Id = "Game7", Game = "Hockey" });
            game.Add(new GameList { Id = "Game8", Game = "Rugby" });
            game.Add(new GameList { Id = "Game9", Game = "Snooker" });
            game.Add(new GameList { Id = "Game10", Game = "Tennis" });
            return game;
        }
    }
}

3. Array of complex object

The MultiSelect 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, Code.Id column and Country.Name column from complex data have been mapped to the value field and text field, respectively.

tagHelper
complexdata.cs
Complex.cs
<div>
    <ejs-multiselect id="country" dataSource="@ViewBag.data" placeholder="Countries" mode="Box">
        <e-multiselect-fields value="Code.Id" text="Country.CountryId"></e-multiselect-fields>
    </ejs-multiselect>
</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 IActionResult complexdata()
        {
            ViewBag.data = new Complex().GetData();
            return View();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class Code {
        public string Id { get; set; }
    }
    public class Complex
    {
        public Country Country { get; set; }
        public Code Code { get; set; }
        public List<Complex> GetData()
        {
            List<Complex> data = new List<Complex>();
            data.Add(new Complex() { Country = new Country() { CountryId = "Australia" }, Code = new Code() { Id = "AU" } });
            data.Add(new Complex() { Country = new Country() { CountryId = "Bermuda" }, Code = new Code() { Id = "BM" } });
            data.Add(new Complex() { Country = new Country() { CountryId = "Canada" }, Code = new Code() { Id = "CA" } });
            data.Add(new Complex() { Country = new Country() { CountryId = "Cameroon" }, Code = new Code() { Id = "CM" } });
            data.Add(new Complex() { Country = new Country() { CountryId = "Denmark" }, Code = new Code() { Id = "DK" } });
            data.Add(new Complex() { Country = new Country() { CountryId = "France" }, Code = new Code() { Id = "FR" } });
            return data;
        }
    }
}

Binding remote data

The MultiSelect 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 MultiSelect.

The following sample displays the first 6 contacts from “Customers” table of the Northwind Data Service.

tagHelper
remotedata.cs
<div id='remote-data'>
    <div class='control-wrapper'>
        <ejs-multiselect id="remote" placeholder="Select customer" query="new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6)">
            <e-multiselect-fields text="ContactName" value="CustomerID"></e-multiselect-fields>
            <e-datamanager url="http://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-datamanager>
        </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 IActionResult remotedata()
        {
            return View();
        }
    }
}

Bind to URL Adaptor

The MultiSelect supports retrieval of data from URL adaptor.

tagHelper
urldata.cs
<div id='url-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-multiselect id="games" placeholder="Select Country" query="new ej.data.Query().select(['CustomerID']).take(7)">
            <e-data-manager adaptor="UrlAdaptor" url="/Multiselect/UrlDatasource"></e-data-manager>
            <e-multiselect-fields text="ShipCountry" value="ShipCountry"></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 IActionResult urldata()
        {
            return View();
        }
        public IActionResult UrlDatasource([FromBody]Data dm)
        {
            var val = OrdersDetails.GetAllRecords();
            var Data = val.ToList();
            var count = val.Count();
            if (dm.where != null)
            {
                Data = (from cust in Data
                        where cust.ShipCountry.ToLower().StartsWith(dm.@where[0].value.ToString())
                        select cust).ToList();
            }
            if (dm.take != 0)
                Data = Data.Take(dm.take).ToList();
            return Json(Data);
        }


        public class Data
        {
            public int take { get; set; }
            public List<Wheres> where { get; set; }
        }

        public class Wheres
        {
            public string field { get; set; }
            public bool ignoreAccent { get; set; }

            public bool ignoreCase { get; set; }

            public bool isComplex { get; set; }

            public string value { get; set; }
            public string Operator { get; set; }

        }
        public class OrdersDetails
        {
            public static List<OrdersDetails> order = new List<OrdersDetails>();
            public OrdersDetails()
            {

            }
            public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
            {
                this.OrderID = OrderID;
                this.CustomerID = CustomerId;
                this.EmployeeID = EmployeeId;
                this.Freight = Freight;
                this.ShipCity = ShipCity;
                this.Verified = Verified;
                this.OrderDate = OrderDate;
                this.ShipName = ShipName;
                this.ShipCountry = ShipCountry;
                this.ShippedDate = ShippedDate;
                this.ShipAddress = ShipAddress;
            }
            public static List<OrdersDetails> GetAllRecords()
            {
                if (order.Count() == 0)
                {
                    int code = 10000;
                    for (int i = 1; i < 10; i++)
                    {
                        order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16), "Kirchgasse 6"));
                        order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123"));
                        order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
                        order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7"));
                        order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S."));
                        code += 5;
                    }
                }
                return order;
            }

            public int? OrderID { get; set; }
            public string CustomerID { get; set; }
            public int? EmployeeID { get; set; }
            public double? Freight { get; set; }
            public string ShipCity { get; set; }
            public bool Verified { get; set; }
            public DateTime OrderDate { get; set; }

            public string ShipName { get; set; }

            public string ShipCountry { get; set; }

            public DateTime ShippedDate { get; set; }
            public string ShipAddress { get; set; }
        }
    }
}

Web API Adaptor

Use the WebApiAdaptor to bind MultiSelect with Web API created using OData.

tagHelper
webapi.cs
<div id='web-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-multiselect id="games" placeholder="Select Country" query="new ej.data.Query().select(['CustomerID']).take(7)">
            <e-data-manager url="/api/Multiselect/" adaptor="WebApiAdaptor"></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
{
    [Route("api/[controller]")]
    public class MultiselectController : Controller
    {
        List<string> game = new List<string>();
        [HttpGet]
        public List<string> Get()
        {
            game.Add("Badminton");
            game.Add("Basketball");
            game.Add("Cricket");
            game.Add("Golf");
            game.Add("Gymnastics");
            game.Add("Tennis");
            game.Add("Hockey");
            return game;
        }
    }
}

Binding with OData services

OData is a standardized protocol for creating and consuming data. You can retrieve data from OData service using the DataManager.

The following example for remote data binding using OData service.

tagHelper
odata.cs
<div id='remote-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-multiselect id="customers" placeholder="Select customer" query="new ej.data.Query().select(['CustomerID']).take(7)">
            <e-multiselect-fields text="CustomerID" value="CustomerID"></e-multiselect-fields>
            <e-data-manager url="http://js.syncfusion.com/ejServices/Wcf/Northwind.svc/Orders/" adaptor="ODataAdaptor" 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 IActionResult odata()
        {
            ViewBag.query = "new ej.data.Query().select(['CustomerID']).take(7)";
            return View();
        }
    }
}

Offline mode

To avoid post back for every action, set the MultiSelect to load all data on initialization and make the actions process in client-side. To enable this behavior, use the Offline property of DataManager.

The following example for remote data binding and enabled offline mode.

tagHelper
offline.cs
<div id='remote-data' class='col-lg-6' style='padding-top:15px'>
    <div class='content'>
        <ejs-multiselect id="customers" placeholder="Select customer" query="new ej.data.Query().select(['CustomerID']).take(7)">
            <e-multiselect-fields text="CustomerID" value="CustomerID"></e-multiselect-fields>
            <e-data-manager url="http://js.syncfusion.com/ejServices/Wcf/Northwind.svc/Orders/" adaptor="ODataAdaptor" offline="true" 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 IActionResult offline()
        {
            ViewBag.query = "new ej.data.Query().select(['CustomerID']).take(7)";
            return View();
        }
    }
}