Value binding in ASP.NET CORE Multi select control

14 Mar 20244 minutes to read

Value binding in the MultiSelect control allows you to associate data values with each list item. This facilitates managing and retrieving selected values efficiently. The MultiSelect component provides flexibility in binding both primitive data types and complex objects.

Primitive Data Types

The MultiSelect Dropdown control provides flexible binding capabilities for primitive data types like strings and numbers. You can effortlessly bind local primitive data arrays, fetch and bind data from remote sources, and even custom data binding to suit specific requirements. Bind the value of primitive data to the value property of the MultiSelect.

Primitive data types include:

  • String
  • Number
  • Boolean
  • Null

The following sample shows the example for preselect values for primitive data type.

<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" allowFiltering="false"  popupHeight="200px">
        </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 primitive()
        {
            ViewBag.data = new string[]{ "Item 1" , "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10" };
            ViewBag.value = new string[] { "Item 5", "Item 14", "Item 25"};
            return View();
        }
    }
}

Object Data Types

In the MultiSelect Dropdown control, object binding allows you to bind to a dataset of objects. When allowObjectBinding is enabled, the value of the control will be an object of the same type as the selected item in the value property. This feature seamlessly binds arrays of objects, whether sourced locally, retrieved from remote endpoints, or customized to suit specific application needs.

The following sample shows the example for preselect values for object data type

<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" allowFiltering="false" allowObjectBinding="true" popupHeight="200px">
            <e-multiselect-fields value="ID" 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 object()
        {
            ViewBag.data = new Record().RecordModelList();
            ViewBag.value = new object[] { new { ID = "id5", Text = "Item 5" }, new { ID = "id14", Text = "Item 14" }, new { ID = "id25", Text = "Item 25" } };
            return View();
        }
    }
}