4

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.

3 rules
  • 1,359
  • 3
  • 26
  • 54

5 Answers5

3

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");
                                    });
                                })
                )
3 rules
  • 1,359
  • 3
  • 26
  • 54
2

The answer you have provided is right actually. The Action must return List<SelectListItem> as the output. See this Example and in the code see the BindTo property.

You can just update your code to below.

        public JsonResult getData()
        {
            try
            {
                var stringlist = object.getstrlist().select( x=> new SelectListItem
                             {
                              Value = x,
                              Text = x
                             }).ToList();


                return Json(stringlist, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

I have just modified your code to not have a for loop.

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
1

Try ValuePrimitive:

                    Html.Kendo().DropDownList()
                        .Name("ddlstrings")
                        .ValuePrimitive(true)
                        .SelectedIndex(0)
                        .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("getData", "String");
                                });
                            })
Jos
  • 310
  • 3
  • 6
0

What does your getData() return? You need to return an enumerable of object that have a property called stringname or what ever property name you specify in the DataText/DataValue fileds. Something like this: return Json(youStringArray.Select(x=>new{stringname = x}))

Alex
  • 3,689
  • 1
  • 21
  • 32
0
using TestSolution.Utility;
 ...

public JsonResult getData()
{
   try
   {
     var stringlist = object.getstrlist();
     return Json(stringlist.ToIdNameList(), JsonRequestBehavior.AllowGet);
   }
   catch (Exception ex)
   {
      return Json("", JsonRequestBehavior.AllowGet);
   }
}

=============================

using TestSolution.Models;
using System.Collections.Generic;
using System.Linq;

namespace TestSolution.Utility
{
    /// <summary>
    /// Kendo Drop Down List Extention
    /// </summary>
    public static class KendoDropDownListExtention
    {
        public static List<IdName> ToIdNameList(this string[] stringList)
        {
            return stringList.Select(sl => new IdName() { Name = sl, Value = sl }).ToList();
        }
    }
}
Thulasiram
  • 8,432
  • 8
  • 46
  • 54