如何绑定 kendo 下拉列表中的字符串列表

How to bind list of string in kendo dropdownlist

您好,我正在为 ASP.NET MVC 使用 Kendo。

我有包含数据的字符串列表

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

现在我想将这个字符串列表绑定到 kendo 下拉列表中。

我已经通过 class 的列表绑定下拉列表,其中包含名称和 ID,但字符串列表中只有一个数据,我不知道如何绑定它!

我是这样做的:

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

但是我没有定义。

我返回的数据是这样的:

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

有谁知道我该怎么做!

如有任何帮助,我们将不胜感激。

你的 getData() return 是什么?您需要 return 具有 属性 的可枚举对象 stringname 或您在 DataText/DataValue 文件中指定的任何 属性 名称。 是这样的: return Json(youStringArray.Select(x=>new{stringname = x}))

不知道好不好,但通过一些手动工作得到了解决方案:

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

在视图侧:

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

您提供的答案实际上是正确的。 Action 必须 return List<SelectListItem> 作为输出。 See this Example 并在代码中看到 BindTo 属性.

您只需将您的代码更新为以下即可。

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

我刚刚修改了您的代码,使其没有 for 循环。

尝试 ValuePrimitive:

                    Html.Kendo().DropDownList()
                        .Name("ddlstrings")
                        .ValuePrimitive(true)
                        .SelectedIndex(0)
                        .DataSource(source =>
                            {
                                source.Read(read =>
                                {
                                    read.Action("getData", "String");
                                });
                            })
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();
        }
    }
}