Kendo UI Dropdown列出数据的默认值

时间:2016-11-02 00:30:21

标签: asp.net vb.net kendo-asp.net-mvc kendo-dropdown

我试图在Read之后根据数据源返回设置Value或SelectedIndex。

这是我的观点

@(Html.Kendo().DropDownList. Name("ddlUsers"). DataTextField("text"). HtmlAttributes(New With {.style = "width:500px"}). DataValueField("id"). DataSource(Sub(s) s.Read(Sub(r) r.Action("GetUserList", "Employees") End Sub). ServerFiltering(True) End Sub).Events(Sub(e) e.Change("SetHiddenUGID")) )

方法GetUserList看起来像这样

Shared Function GetUserList() As IList Return db.GetDBUserList().Where(Function(w) w.value <> 0).Select(Function(s) New With {.id = s.value,.text = s.text,.isdefault = s.isdefault}).ToList() End Function

现在GetDBUserList返回员工列表

Public Class Employees Public Property value As Int64 Public Property text As String Public Property isdefault As Int32 End Class

我想根据isdefault设置下拉列表的默认值,当它为1时,是否有任何想法?

我试过了

var dropdownlist = $("#ddlUsers").data("kendoDropDownList");

dropdownlist.select(function (dataItem) { if (dataItem.isdefault === 1) { $("#ddlUsers").data("kendoDropDownList").value(dataItem.id); } });

但它不起作用。

1 个答案:

答案 0 :(得分:0)

您的DropDownList绑定到远程数据,因此在读取操作后返回数据时,您无法尝试设置该值。

因此,您必须添加代码以选择DropDownList的dataBound事件中的默认项,因为在此之前没有要选择的数据。

但是,您的函数正在尝试将值设置为dataItem.id ...您的模型没有id字段...它具有值,文本和默认值。

此外,DropDownList select()方法接受jQuery选择器,索引或返回布尔值(http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#methods-select)的函数。你的函数没有返回任何东西......所以它会导致没有被选中。

尝试在DropDownList的dataBound事件中执行此操作:

using System;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;

namespace Gen2Collections
{
    class Program
    {
        private static PerformanceCounter _privateBytesCounter;
        private static PerformanceCounter _gen2Counter;

        static void Main(string[] args)
        {
            _privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", Process.GetCurrentProcess().ProcessName);
            _gen2Counter = new PerformanceCounter(".NET CLR Memory", "# Gen 2 Collections", Process.GetCurrentProcess().ProcessName);
            RunMonitor();
            Thread.Sleep(60000);
        }
        private static void RunMonitor()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    _privateBytesCounter.NextValue();
                    Console.WriteLine(_gen2Counter.NextValue());
                    Thread.Sleep(1000);
                }
            });
        }
    }
}

这是一个有效的例子:http://dojo.telerik.com/@Stephen/inUKI 它使用javascript初始化,但您可以轻松地将dataBound事件处理程序添加到Razor初始化。