【问题标题】:Kendo dropdownlist produces TypeError: n.slice is not a functionKendo 下拉列表产生 TypeError: n.slice is not a function
【发布时间】:2014-07-26 19:17:42
【问题描述】:

我需要定义架构吗?如果是这样,那应该是什么样子?我对此的搜索似乎只找到了 js 解决方案,我正在寻找在编辑器模板中定义它的语法。

共享/编辑器模板:

@(
Html.Kendo().DropDownList()
.Name("SearchFunction")
.DataTextField("SearchFunctionDesc")
.DataValueField("SearchFunctionCode")
.DataSource(source =>
    {        
        source.Read(read => { 
            read.Action("GetSearchFunctions", "User");
        });
    })
    .OptionLabel("--Select a Search Function--")
    .AutoBind(false)
)

在控制器中:

    public JsonResult GetSearchFunctions([DataSourceRequest] DataSourceRequest request)
    {
        var searchFuncs = AdminService.GetSearchFunctions();
        DataSourceResult result = searchFuncs.ToDataSourceResult(request);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

然后是我的 Dapper 数据库查询:

            var result = new List<SearchFunction>();
            using (var conn = new OracleConnection(DatabaseConnectionString))
            {
                conn.Open();
                string query = "select FUNCTION_ID, SEARCH_FUNCTION_CD, " +        
                        "SEARCH_FUNCTION_DESC, IS_ACTIVE " +
                         "from TBL_SEARCH_FUNCTIONS "; 
                result = conn.Query(query)
                    .Select(s => new SearchFunction
                    {
                        FunctionId = (int)s.FUNCTION_ID,
                        SearchFunctionCode = s.SEARCH_FUNCTION_CD,
                        SearchFunctionDesc = s.SEARCH_FUNCTION_DESC,
                        Active = s.IS_ACTIVE
                    }).ToList<SearchFunction>();
                conn.Close();
                return result;
            }

【问题讨论】:

  • DataSourceRequest 对于下拉菜单不是必需的。删除该部分并返回searchFuncs。也知道你的场景是什么,但你可能想设置AutoBind(true)
  • 这样做有什么意义?将所有数据发送给客户端?合乎逻辑吗?

标签: asp.net asp.net-mvc c#-4.0 kendo-grid kendo-asp.net-mvc


【解决方案1】:

像这样重写你的控制器方法:

public JsonResult GetSearchFunctions()
{
    var searchFuncs = cmsViewAdminService.GetSearchFunctions();
    return Json(searchFuncs, JsonRequestBehavior.AllowGet);
}

这应该简化该方法,因为您不需要 DataSourceRequest (正如评论中提到的@CSharper)。 Kendo DropDownLists 与网格不同,不需要 DataSourceRequest 类。这样,如果需要,您可以从 jQuery Ajax 方法调用相同的 JsonResult。

【讨论】:

  • 如果是这样,服务器过滤是如何工作的?我认为这里有问题。在这种情况下,您没有进行服务器过滤。
【解决方案2】:

你需要从 json 中返回一个看起来像这样的纯集合

{[
    {"Id":2,"Name":"some"},
    {"Id":3,"Name":"som2"}
]}

【讨论】:

    【解决方案3】:

    如果你使用 ModelView 和 lambda 那么你也可以试试下面的代码:

    假设您有一个从参考表(填充到 CityViewModel)中检索数据的城市下拉列表:

    public JsonResult GetCities()
    {
        var dataContext = new EFDbContext();
    
        var cities = dataContext.Cities.Select(c => new CityViewModel
        {
            ID = c.ID,
            Name = c.Name
        });
        return Json(cities, JsonRequestBehavior.AllowGet);
    }
    

    问候。

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2014-09-03
      相关资源
      最近更新 更多