Sorting in ASP.NET MVC ComboBox

27 Jul 20223 minutes to read

The ComboBox has built-in support to sort data items when SortOrder is specified. The available sort orders are ascending, descending and none.

To sort the data in ascending order, set the SortOrder as ascending.

The following sample illustrates how to sort the data.

  • HTML
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("Sorting")
                        .Datasource((IEnumerable<Countries>)ViewBag.datasource)
                        .Width("100%")
                        .ComboBoxFields(fields => fields.Text("text"))
                        .Placeholder("Select a country")
                        .SortOrder(SortOrder.Ascending)
                        .Render();
                }
            </div>
        </div>
  • C#
  • public string text { get; set; }
            public string category { get; set; }
            public static List<Countries> GetCountries()
            {
                List<Countries> country = new List<Countries>();
                country.Add(new Countries { text = "Austria", category = "A" });
                country.Add(new Countries { text = "Australia", category = "A" });
                country.Add(new Countries { text = "Antarctica", category = "A" });
                country.Add(new Countries { text = "Bangladesh", category = "B" });
                country.Add(new Countries { text = "Brazil", category = "B" });
                country.Add(new Countries { text = "Canada", category = "C" });
                country.Add(new Countries { text = "Cuba", category = "C" });
                country.Add(new Countries { text = "Denmark", category = "D" });
                country.Add(new Countries { text = "Dominica", category = "D" });
                return country;
            }
        }
    }

    Output for Sorting is as follows.

    ASP.NET MVC ComboBox sorting