3 rules
3 rules

Reputation: 1409

How to bind list of string in kendo dropdownlist

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.

Upvotes: 4

Views: 16008

Answers (5)

Thulasiram
Thulasiram

Reputation: 8552

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();
        }
    }
}

Upvotes: 0

Jos
Jos

Reputation: 310

Try ValuePrimitive:

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

Upvotes: 1

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

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.

Upvotes: 2

3 rules
3 rules

Reputation: 1409

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");
                                    });
                                })
                )

Upvotes: 3

Alex
Alex

Reputation: 3889

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}))

Upvotes: 0

Related Questions