Combo Box for ASP.NET MVC - How to filter a large data source and automatically select the first item
This example demonstrates how to use the combo box editor's BindList method to bind the editor to a large filtered data source.
Use the editor's BindList method and pass the following methods as parameters:
-
A delegate method of the ItemsRequestedByFilterConditionMethod type. This method allows you to implement custom item selection based on filter conditions.
-
A delegate method of the ItemRequestedByValueMethod type. This method allows you to implement custom item selection based on items' values.
var comboBox = Html.DevExpress().ComboBox(settings => {
settings.Name = "comboBox";
<!-- ... -->
}).BindList(ComboBoxLargeDataBase.Model.DataHelper.GetPersonsRange, ComboBoxLargeDataBase.Model.DataHelper.GetPersonByID);
public static object GetPersonsRange(ListEditItemsRequestedByFilterConditionEventArgs args) {
var skip = args.BeginIndex;
var take = args.EndIndex - args.BeginIndex + 1;
return (from person in DataHelper.Persons
where (person.FirstName + " " + person.MiddleName + " " + person.LastName).ToLower().Contains(args.Filter.ToLower())
orderby person.LastName
select person
)
.Skip(skip)
.Take(take);
}
public static object GetPersonByID(ListEditItemRequestedByValueEventArgs args) {
if(args.Value != null)
return GetPersonByID((int)args.Value);
return null;
}
When a single item remains available after filter operations, the editor's SetSelectedIndex method automatically selects it in the editor's client-side EndCallback event handler.
- HomeController.cs (VB: HomeController.vb)
- MyModel.cs (VB: MyModel.vb)
- ComboBoxPartial.cshtml
- Index.cshtml
(you will be redirected to DevExpress.com to submit your response)