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

sort dataSource in @(Html.Kendo().MultiSelect()

4 Answers 466 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 04 Sep 2018, 06:39 AM

I'm setting up a MultiSelect as shown below.
How can setup sorting of the DataSource?

@(Html.Kendo().MultiSelect()
    .Name("Country")
    .DataTextField("Country")
    .DataValueField("CountryId")
    .Filter(FilterType.Contains)
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetCountries", "Helper").Data("{ any: false }");
        });
    })
)


4 Answers, 1 is accepted

Sort by
0
Veselin Tsvetanov
Telerik team
answered on 06 Sep 2018, 06:09 AM
Hello Morten,

In order to configure the Sort() rule for the MultiSelect DataSource, you will have to use its Custom() option:
@(Html.Kendo().MultiSelect()
    .Name("Country")
    .DataTextField("Country")
    .DataValueField("CountryId")
    .Filter(FilterType.Contains)
    .DataSource(source => source
        .Custom()
        .Transport(t => t
            .Read(r => r
                .Action("GetCountries", "Home")
                .Data("{ any: false }")
            )
        )
        .Sort(s => s
            .Add("Country")
            .Descending()
        )
    )
)

and:
public ActionResult GetCountries()
{
    var result = new object[]
    {
        new { CountryId = 1, Country = "Bulgaria" },
        new { CountryId = 2, Country = "France" },
        new { CountryId = 3, Country = "Uganda" }
    };
 
    return Json(result, JsonRequestBehavior.AllowGet);
}

I hope that the above helps you. Should you have any other questions, please do not hesitate to contact us.

Regards,
Veselin Tsvetanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 07 Sep 2018, 07:44 PM

Thanks Veselin,

only if I setup the .DataSource this way can I make it work:

.DataSource(source => source
    .Custom()
    .Sort(s => s.Add("CountryName"))
    .Transport(t => t
        .Read(read =>
        {
            read.Action("GetCountries", "Helper");
        })
    )
)

And I can make the sending of the parameter data (.Data("{ any: false }")) work.

Could you try to add bool any as parameter to the GetCountries method and show how to pass the parameter?

public ActionResult GetCountries(bool any)
{
    var result = new object[]
    {
        new { CountryId = 1, Country = "Bulgaria" },
        new { CountryId = 2, Country = "France" },
        new { CountryId = 3, Country = "Uganda" }
    };

    return Json(result, JsonRequestBehavior.AllowGet);
}

Thanks

/Morten

 

0
Veselin Tsvetanov
Telerik team
answered on 11 Sep 2018, 02:33 PM
Hi Morten,

Attached you will find a simple MVC project implementing the discussed scenario. The GetCountries() controller action accepts a parameter of type bool with name any:
public ActionResult GetCountries(bool any)
{
    var result = new object[]
    {
        new { CountryId = 1, Country = "Bulgaria" },
        new { CountryId = 2, Country = "France" },
        new { CountryId = 3, Country = "Uganda" }
    };
 
    return Json(result, JsonRequestBehavior.AllowGet);
}

It also properly returns the sorted data to the widget.

Regards,
Veselin Tsvetanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 13 Sep 2018, 10:16 AM
Thanks Veselin. That worked
Tags
DropDownList
Asked by
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Veselin Tsvetanov
Telerik team
Morten
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or