You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core中KendoUI ComboBox无法显示的解决方法(无需改脚本位置)

问题原因

你的脚本加载顺序是页面内容(@RenderBody())先渲染,之后才加载jQuery和Kendo库。而Html.Kendo().ComboBoxFor()生成的组件初始化代码会直接输出到页面中,此时Kendo和jQuery还未加载,无法找到对应的API,导致组件无法正常初始化渲染。

解决方案

方案1:使用Kendo延迟初始化(Deferred)

这是适配ASP.NET MVC Kendo助手的最优方案,无需调整脚本位置:

  1. 修改Create.cshtml中的ComboBox配置,添加.Deferred()方法延迟初始化:
@(Html.Kendo().ComboBoxFor(x => x.CarId)
    .Placeholder("Select Car")
    .DataTextField("Text")
    .DataValueField("Value")
    .DataSource(source =>
    {
        source.Read(r => r.Action("Car_Read", "Service"));
    })
    .Filter(FilterType.StartsWith)
    .Deferred() // 新增该行,延迟组件初始化
 )
  1. 在Create.cshtml末尾添加Scripts节,调用延迟初始化脚本:
@section Scripts {
    @Html.Kendo().DeferredScripts()
}

该方式会将组件初始化代码延迟到所有依赖库加载完成后执行,确保Kendo API可用。

方案2:手动编写JS初始化代码

如果不想使用Deferred机制,可手动控制初始化时机:

  1. 先在页面中生成基础的select标记:
<select id="CarId" name="CarId" class="form-control k-rtl"></select>
  1. 在Create.cshtml的Scripts节中编写初始化逻辑:
@section Scripts {
    <script>
        $(function() {
            $("#CarId").kendoComboBox({
                placeholder: "Select Car",
                dataTextField: "Text",
                dataValueField: "Value",
                dataSource: {
                    transport: {
                        read: {
                            url: "@Url.Action("Car_Read", "Service")"
                        }
                    }
                },
                filter: "startswith"
            });
        });
    </script>
}
额外优化

你当前同时加载了kendo.all.min.js和kendo.web.min.js,前者已包含后者的全部功能,重复加载可能引发冲突,建议移除kendo.web.min.js的引用,减少不必要的资源加载。

内容的提问来源于stack exchange,提问作者Javad-M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:44:54