How to bind list of string in kendo dropdownlist

3 rules picture 3 rules · Jul 12, 2016 · Viewed 10.9k times · Source

Hello I am using Kendo for ASP.NET MVC.

I have list of string containing data

[0]="str1"
[1]="str2"... and so on

Now I want to bind this list of string into kendo dropdownlist.

I have bind dropdownlist by list of class with name and id but with only one data in list of string, I don't know how to bind that!

I have done that like below:

 @(
                 Html.Kendo().DropDownList()
                            .Name("ddlstrings")
                            .DataTextField("stringname")
                            .DataValueField("stringname")
                            //.Events(x => x.Select("sourceclick"))
                            .SelectedIndex(0)
                            .DataSource(source =>
                                {
                                    source.Read(read =>
                                    {
                                        read.Action("getData", "String");
                                    });
                                })
                )

But I got undefined.

I am returning the data like this:

public JsonResult getData()
        {
            try
            {
                List<string> stringlist = object.getstrlist();
                return Json(stringlist, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

Does anyone have any idea how can I do this!

Any help would be appreciate.

Answer

3 rules picture 3 rules · Jul 12, 2016

Don't know is it good or not but got the solution with some manual work:

var selectList = new List<SelectListItem>();

foreach (var element in stringlist)
                {
                    selectList.Add(new SelectListItem
                    {
                        Value = element.ToString(),
                        Text = element.ToString()
                    });
                }

return Json(selectList, JsonRequestBehavior.AllowGet);

and at view side:

@(
                 Html.Kendo().DropDownList()
                            .Name("ddlstrings")
                            .DataTextField("Text")
                            .DataValueField("Value")
                            //.Events(x => x.Select("sourceclick"))
                            .SelectedIndex(0)
                            .DataSource(source =>
                                {
                                    source.Read(read =>
                                    {
                                        read.Action("getData", "String");
                                    });
                                })
                )