自定义Kendo下拉列表消息以替换“未找到数据”

问题描述

在我们的.NET Core 3 Web应用程序中,我们将剑道与剃刀视图一起使用。

我们正在使用Html.Kendo().DropDownListFor控件,该控件在没有要显示的数据时会显示文本:

“找不到数据”

我们要用自定义消息替换此字符串。我们知道我们可以替换kendo.all.min.js中的字符串,但是我们知道一旦更新Kendo,它将被覆盖。

我们如何在DropDownListFor控件本身中以编程方式指定文本字符串?

解决方法

您可以设置和分配模板以使用和自定义无数据消息。我还为跨度添加了一种样式,以删除Kendo添加的大写文本转换。像这样:

<!-- Template -->
<script id="noDataTemplate" type="text/x-kendo-template">
    <span style="text-transform: none;">
        <strong>My Custom No Data Message here</strong>
    </span>
</script>

<!-- DropDownList initialization -->
@(Html.Kendo().DropDownList()
            .Name("customers")
            .DataTextField("ContactName")
            .DataValueField("CustomerID")
            .NoDataTemplateId("noDataTemplate") // Reference to the template.
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("Template_GetCustomers","ComboBox");
                });
            })
)

此处提供更多信息:https://docs.telerik.com/aspnet-core/html-helpers/editors/dropdownlist/templates#no-data-templates

,

我建议您为项目创建一个JS文件并覆盖所需的任何消息,以便可以毫无问题地更新kendo。

现在,要回答您的问题,在DropDownList的情况下,您可以像下面这样更改以下属性的值:

kendo.ui.DropDownList.prototype.options.messages.noData = 'Sorry,no data here';

您可以使用任何其他小部件来做到这一点。