Search results

Enable/Disable Items in ASP.NET MVC ListBox control

To enable or disable items in the list box, enableItems method can be used. In the following example, the Bugatti Veyron Super Sport and SSC Ultimate Aero items are disabled by default and by clicking Enable Items buttons, the disabled items will be enabled.

razor
enableitem.cs
@Html.EJS().ListBox("listbox").DataSource((IEnumerable<object>)ViewBag.data).Created("create").Render()
<button class="e-btn" id="enableitem">Enable Items</button>

<script>
    var disableItems = ['Bugatti Veyron Super Sport', 'SSC Ultimate Aero'];
    function create() {
        var listboxobj = document.getElementById("listbox").ej2_instances[0];
        listboxobj.enableItems(disableItems, false);
    }

    document.getElementById('enableitem').onclick = function () {
        var listboxobj1 = document.getElementById("listbox").ej2_instances[0];
        listboxobj.enableItems(disableItems, true);
    }
</script>
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 enableitem()
        {
            ViewBag.data = new string[] { "BadmHennessey Venominton", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Bugatti Chiron", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220" };
            return View();
        }
    }
}