This is a migrated thread and some comments may be shown as answers.

How do I sort the .DataSource when using DropDownListFor

4 Answers 1967 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
Morten asked on 18 Aug 2015, 08:17 AM
I build and bind a DropDownList as listed below.

The "GetItemCategories" returns data I would like to sort before displayed in the DropDownList.

How do I do the sorting?

 
@(Html.Kendo().DropDownListFor(m => m.Category)
    .Name("Category")
    .DataTextField("Name")
    .DataValueField("Value")
    .HtmlAttributes(new { style = "width: 280px;" })
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetItemCategories", "Helper");
        });
    })
    .Value(Model.ActionTimeFrameId.ToString())
)

 

 

 

4 Answers, 1 is accepted

Sort by
1
Boyan Dimitrov
Telerik team
answered on 20 Aug 2015, 07:43 AM

Hello Morten,

Please use custom transport as shown below: 

@(Html.Kendo().DropDownList()
         .Name("products")
         .HtmlAttributes(new { style = "width: 250px" })
         .DataTextField("ProductName")
         .DataValueField("ProductID")
         .DataSource(source =>  source
               .Custom()
               .Sort(s => s.Add("ProductName"))
               .Transport(tr => tr
                   .Read(read =>
                   {
                       read.Action("GetProducts", "Home");
                   })))
        )

 

Regards,
Boyan Dimitrov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 20 Aug 2015, 12:44 PM
thanks a lot
0
Steve
Top achievements
Rank 1
Veteran
answered on 14 Jan 2021, 06:43 PM
Why do you need a custom transport when the grid has Sort without having to do that?
1
Aleksandar
Telerik team
answered on 18 Jan 2021, 01:50 PM

Hello Steve,

The DropDownList can be used independently from the Grid, as a separate widget, and the provided suggestion shows how to sort the data before displaying it in the DropDownList. 

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DropDownList
Asked by
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Boyan Dimitrov
Telerik team
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
Steve
Top achievements
Rank 1
Veteran
Aleksandar
Telerik team
Share this question
or