# Cascade DropDownLists with Enabled Virtualization

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>DropDownList for Telerik UI for ASP.NET MVC</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

## Description

How can I implement cascading ASP.NET MVC DropDownLists with enabled virtualization?

## Solution

To implement cascading DropDownLists and still virtualize the data, you must use a [custom DataSource](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/datasource/types) configuration. It enables the usage of the `DataSourceRequest` and `ToDataSourceResult` methods, which internally parse all filter and page expression information, and apply it directly to the data.

```Razor HtmlHelper
@(Html.Kendo().DropDownList()
    .Name("Category")
    .BindTo(ViewBag.Categories)
    .DataTextField("CategoryName")
    .DataValueField("CategoryId")
)

<h5>Type "4" in the filter input</h5>

@(Html.Kendo().DropDownList()
    .Name("SelectEmployeeId")
    .CascadeFrom("Category")
    .DataValueField("Id")
    .DataTextField("Name")
    .Filter("contains")
    .Virtual(v => v.ValueMapper("SelectEmployeeValueMapper").ItemHeight(26))
    .DataSource(source =>
    {
        source.Custom()
        .ServerFiltering(true)
        .ServerPaging(true)
        .Type("aspnetmvc-ajax")
        .Transport(transport => transport.Read(r => r.Action("GetVirtualData", "Home")))
        .Schema(schema =>
        {
            schema.Data("Data").Total("Total");
        });
    })
)
```

```JavaScript Scripts
<script>
    // The valueMapper function is called when you want to select a data item that is not present in the DataSource.
    function SelectEmployeeValueMapper(options) {
        $.ajax({
            url: "@Url.Action("EmployeeValueMapper", "Home")",
            data: {
                value: options.value || 0,
                status: 1,
                excludeId: -1
            },
            success: function (data) {
                options.success(data);
            }
        });
    }
</script>
```

```C# Controller
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var categories = Enumerable.Range(1, 5).Select(i => {
            return new Category
            {
                CategoryId = i,
                CategoryName = "Category " + i.ToString()
            };
        });
        ViewBag.Categories = categories;
        return View();
    }

    public JsonResult GetVirtualData([DataSourceRequest] DataSourceRequest request)
    {
        var data = GetEmployee();
        return Json(data.ToDataSourceResult(request));
    }

    private static List<Employee> GetEmployee()
    {
        var rand = new Random();

        var data = Enumerable.Range(0, 1000).Select(i => {
            return new Employee
            {
                Id = i,
                CategoryId = rand.Next(1, 5),
                Name = "Emp" + i.ToString()
            };
        }).ToList();

        return data;
    }

    public JsonResult EmployeeValueMapper(int value)
    {
        var dataItemIndex = -1;
        var data = GetEmployee();
        if (value != 0)
        {
            var index = 0;
            foreach (var vm in data)
            {
                if (vm.Id == value)
                {
                    dataItemIndex = index;
                    break;
                }

                index += 1;
            }
        }
        return Json(dataItemIndex, JsonRequestBehavior.AllowGet);
    }
}
```

To see the complete example, refer to the ASP.NET MVC project on how to [cascade DropDownLists with enabled virtualization](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/DropDownListVirtualization).

## More ASP.NET MVC DropDownList Resources

* [ASP.NET MVC DropDownList Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/dropdownlist/overview)

* [ASP.NET MVC DropDownList Demos](https://demos.telerik.com/aspnet-mvc/dropdownlist)

* [ASP.NET MVC DropDownList Product Page](https://www.telerik.com/aspnet-mvc/dropdownlist)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the DropDownList for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/dropdownlist)

* [Server-Side API Reference of the DropDownList for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/dropdownlist)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
