基于Kendo DropDownList选择提供Kendo自动完成数据

时间:2013-10-30 16:38:48

标签: asp.net-mvc-4 razor kendo-ui

我有一个Kendo自动完成控件,它应该根据父Kendo-dropdownlist的选定值填充其数据。

我想知道如何去做这件事。我正在使用MVC 4和Razor视图。

我试图这样做,但这不起作用:

<div class="editor-field">
    @Html.Kendo().DropDownListFor(x => x.CustomerId).Name("customerDropDownList").Events(e=>e.Close("selectedItem")).Text(ViewBag.Customers[Model.CustomerId].TextValue).AutoBind(false).DataTextField("TextValue").DataValueField("Id").BindTo(ViewBag.Customers).HtmlAttributes(new { style = "width: 215px" })
</div>

<div class="editor-field">
    @Html.Kendo().AutoComplete().Name("customerOrders").Filter("startsWith").Placeholder("Customer Order Number.")
</div>


<script>
    $('#customersDropDownList').kendoDropDownList({
       close : function selectItem (e) {
            var item = e.item;
            var text = item.text();
            // Use the selected item or its text
        }
    });
</script>

1 个答案:

答案 0 :(得分:0)

在下拉列表的更改(或onClose或OnSelect)事件中,您可以使用本地或远程数据设置自动完成的数据源,如:

<div class="editor-field">
    @Html.Kendo().DropDownListFor(x => x.CustomerId).Name("customerDropDownList").Events(e=>e.Change("onDropdownChange")).Text(ViewBag.Customers[Model.CustomerId].TextValue).AutoBind(false).DataTextField("TextValue").DataValueField("Id").BindTo(ViewBag.Customers).HtmlAttributes(new { style = "width: 215px" })
</div>

<div class="editor-field">
    @Html.Kendo().AutoComplete().Name("customerOrders").Filter("startsWith").Placeholder("Customer Order Number.")
</div>    

<script>
    function onDropdownChange(e) {

        var selectedValue = this.value();    
        // If you have local data for every dropdown option, wrap them in individual datasource and switch accordingly.
        var dataSourceForoption1 = kendo.data.DataSource({ data: ["John", "Adam", "binbsr:)", "ginni"] });                        
        var autocomplete = $("#customerOrders").data("kendoAutoComplete");
        autocomplete.setDataSource(dataSourceForoption1); // For now, same dataSource for every option

    }    

</script>

希望它有所帮助。