【问题标题】:Accessing filtersource in MVC Razor syntax在 MVC Razor 语法中访问过滤器源
【发布时间】:2013-11-27 15:36:27
【问题描述】:

如何在如下所示的 MVC Razor 中实现 change:function(e) 中的内容?几天来,我一直在敲打我的头,找不到任何东西。

$(document).ready(function() {
var items = [
    {value : '1',
     desc : 'fred'},
    {value : '2',
     desc : 'wilma'},
    {value : '3',
     desc : 'pebbles'},
    {value : '4',
     desc : 'dino'}
];

var cb = $('#comboBox').kendoComboBox({
    dataSource : items,
    dataTextField : 'desc',
    dataValueField : 'value',
    filter : 'contains',
    change : function (e) {
        if (this.value() && this.selectedIndex == -1) {    
            this._filterSource({
                value: "",
                field: this.options.dataTextField,
                operator: "contains"
            });
            this.select(1);
        }
    }
}).data('kendoComboBox');

$('#showValue').click(function () {
    alert(cb.value());
});

});

这里的工作示例 --> JSFiddle


这是我目前所拥有的。 if 语句中的内容没有任何作用。

function onChange(e) {
    var lT = $("#loadType").data("kendoComboBox").input.val();
    if (lT != "Generic") {
        // Here I need to compare what's entered to what is populated in the comboBox dropdown
        if (this.selectedIndex == -1) {
            this._filterSource({ 
                value: "",
                field: this.options.dataTextField,
                operator: "contains"
            });
            this.select(1);

                //this is for Testing purposes only
                alert("See Me?");
            }

        //this is for Testing purposes only
        alert(lT + " " + this.selectedIndex + " " + this.value());
    }
};

为了完整起见,这里也是组合框:

<div class="form-group">
        @Html.LabelFor(model => model.loadDescrip, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @(Html.Kendo().ComboBox()
                    .Name("loadDescrip")
                    .Filter(FilterType.Contains)
                    .DataTextField("DocCode")
                    .DataValueField("DocCode")
                    .DataSource(source =>
                    {
                        source.Read(read =>
                        {
                            read.Action("GetCascadeDocumentNumbers", "DockDoor")
                                .Data("filterLoadDescription");
                        })
                      .ServerFiltering(true);
                    })
                    .Enable(false)
                    .AutoBind(false)
                    .CascadeFrom("loadType")
                    .Events(e =>
                        {
                e.Change("onChange");
                        })
            )
            @Html.ValidationMessageFor(model => model.loadDescrip)
        </div>
    </div>

【问题讨论】:

  • 实际问题是什么?您需要将其转换为服务器包装语法吗?
  • 是的,特别是更改事件。我似乎无法弄清楚如何在 Server-Wrapper 语法中实现其中的内容。
  • 问题是当您尝试使用“loadType”获取组合框时,它的名称为“loadDescript”。
  • 不,“loadType”是一个不同的组合框。它们处于级联关系中。我把它排除在外,认为它没有关系。可以包括如果它会帮助。我就是无法让_filterSourceselect(1) 工作。他们似乎什么都没做。

标签: c# asp.net-mvc kendo-ui asp.net-mvc-5


【解决方案1】:

您可以通过两种方式在 Razor 中处理事件:

  1. 指定事件处理程序的名称

    @(Html.Kendo().ComboBox()
          .Name("combobox")
          .Events(events => events.Change("combobox_change"))
    )
    <script>
    function combobox_change(e) {
      // handle the event
    }
    </script>
    
  2. 将事件处理程序定义为 Razor 模板委托

    @(Html.Kendo().ComboBox()
      .Name("combobox")
      .Events(events => events.Change(@<text>
          function(e) {
             // handle the event
          }
      </text>))
    )
    

更多信息可以在这里找到:http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/combobox/overview#handling-kendo-ui-combobox-events

【讨论】:

  • 对不起,我现在不是很清楚。我有一个事件,我只是无法让示例事件中的代码在我的版本中工作。我将用我的代码修改问题。感谢您的帮助!
猜你喜欢
  • 2020-11-26
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多