“uncaught TypeError:e.slice不是函数”,同时将json数据传递给“orchard”中的视图

时间:2015-08-19 05:06:19

标签: javascript json

public ActionResult Grouping()
        {
            return View();
        }

public ActionResult Read([DataSourceRequest] DataSourceRequest request, string text)
        {


            var result = _auto.Table.ToList().Where(s => s.Charactres.Contains(text));

            return Json(result.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);


        }

view code:`enter code here`




@model IEnumerable<Autocomplete.Models.Autocomplete>
@{
    ViewBag.Title = "Grouping";
}

<h2>Grouping</h2>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="~/Modules/Autocomplete/Scripts/kendo/2014.3.1119/jquery.min.js"></script>
<script src="~/Modules/Autocomplete/Scripts/kendo/2014.3.1119/kendo.all.min.js"></script>
<script src="~/Modules/Autocomplete/Scripts/kendo/2014.3.1119/kendo.aspnetmvc.min.js"></script>
<link href="~/Modules/Autocomplete/Content/kendo/2014.3.1119/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="~/Modules/Autocomplete/Content/kendo/2014.3.1119/kendo.default.min.css" rel="stylesheet" type="text/css" />
<link href="~/Modules/Autocomplete/Content/kendo/2014.3.1119/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
<link href="~/Modules/Autocomplete/Content/kendo/2014.3.1119/kendo.mobile.all.min.css" rel="stylesheet" type="text/css" />
<link href="~/Modules/Autocomplete/Content/kendo/2014.3.1119/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />
<div class="demo-section">
    <h4>Customers</h4>

    @(Html.Kendo().AutoComplete()
              .Name("data")
              .DataTextField("ContactName")
              .Filter("contains")
              .MinLength(3)
              .HtmlAttributes(new { style = "width:250px" })
              .Placeholder("Type a customer name")
              .DataSource(source => source
                  .Custom()
                  //.Group(g => g.Add("Country", typeof(string)))
                  .Transport(transport => transport
                    .Read(read =>
                    {
                        read.Action("Read", "AutoComplete")
                            .Data("onAdditionalData");
                    }))
                .ServerFiltering(true))
    )
</div>
<script>
    function onAdditionalData() {
        return {
            text: $("#data").val()
        };
    }
</script>

3 个答案:

答案 0 :(得分:3)

尝试从控制器方法返回结果,而不使用ToDataSourceResult()方法。这似乎发送数据而不包装客户端javascript在对象内寻找的数组。

所以你的控制器返回语句看起来像这样:

return Json(result, JsonRequestBehavior.AllowGet);

我从另一个stackoverflow回答中得到了这个解决方案: Error rendering data with Javascript / KendoUI autocomplete - Object #<Object> has no method 'slice' - how to resolve?

这个解决方案对我有用。 祝你好运!

答案 1 :(得分:0)

对我来说,解决方案是将模型(架构)定义为与接收到的数据相同。

对于我的情况(ASP.NET MVC),我使用正确的架构设置了自定义数据源:

.DataTextField("description")
.DataValueField("name")
.DataSource(dataSource => dataSource
    .Custom()
    .Type("aspnetmvc-ajax")
    .ServerPaging(false)
    .ServerSorting(false)
    .ServerFiltering(false)
    .Transport(transport => transport
        .Read(read => read.Action("Get_Users", "User"))
    )
    .Schema(schema => schema
        .Data("Data")
        .Model(model =>
        {
            model.Id("description");
            model.Field("name", typeof(string));
        })
    )
)

参考: https://www.telerik.com/forums/uncaught-typeerror-e-slice-is-not-a-function-on-data-fetch

Error rendering data with Javascript / KendoUI autocomplete - Object #<Object> has no method 'slice' - how to resolve?

答案 2 :(得分:0)

以下更改对我有用,在dataSource的{​​{1}}之后添加了架构:

kendoGrid