Search results

Get the total count of data when remote data bind with DropDownList in ASP.NET MVC DropDownList control

05 Aug 2021 / 1 minute to read

Before control rendering, you can get the total items count by using actionComplete event with its result arguments. After rendering this control, you can get the total items count by using getItems method.

The following example demonstrate how to get the total items count.

razor
totalcount.cs
Copied to clipboard
@Html.EJS().Button("btn").Content("Get Items").Render()
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).Fields(new DropDownListFieldSettings
            {
                Text = "ContactName",
                Value = "CustomerID"
            }).Query((string)ViewBag.query).Render()
<script>
 function actionComplete (e) {
         // get total items count
         console.log("Total items count: " + e.result.length);
         var element = document.createElement('p');
         element.innerText = "Total items count: " + e.result.length;
         document.getElementById('event').append(element);
    }
    document.getElementById('btn').onclick = () => {
        var Obj = document.getElementById("customers").ej2_instances[0];
        // get items count using getItems method
        console.log("Total items count: " + Obj.getItems().length);
        let element = document.createElement('p');
        element.innerText = "Total items count: " + Obj.getItems().length;
        document.getElementById('event').append(element);
    };
</script>
Copied to clipboard
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 DropDownListController : Controller
    {
        public ActionResult totalcount()
        {
            return View();
        }
    }
}