2

我正在尝试在服务器端处理 Kendo UI ComboBox 的过滤。我在视图中有以下代码

$('#Solicitor').kendoComboBox({
    placeholder: "@T("Enter the partial Name or Primary ID of the Entity.").Text",
    dataTextField: "text",
    dataValueField: "id",
    autoBind: false,
    minLength: 3,
    filter: "startswith",
    dataSource: {
        serverFiltering: true,
        transport: {
            read: {
                url: "@Url.Action("Index", "EntitiesAdmin", new { area = "BizNet.Entity"})",
                dataType: "json"
            }
        }
    }
});

当我在 ComboBox 中输入一些内容时,例如。CAR,使用 Fiddler2,我可以看到以下查询字符串正在发送到服务器

filter%5Blogic%5D=and&filter%5Bfilters%5D%5B0%5D%5Bvalue%5D=CAR&filter%5Bfilters%5D%5B0%5D%5Bfield%5D=text&filter%5Bfilters%5D%5B0%5D%5Boperator%5D=startswith&filter%5Bfilters%5D%5B0%5D%5BignoreCase%5D=true

解析后,它看起来像这样

filter[logic]:and
filter[filters][0][value]:CAR
filter[filters][0][field]:text
filter[filters][0][operator]:startswith
filter[filters][0][ignoreCase]:true

从外观上看,ComboBox 正在通过 ajax 向服务器发送一个名为filter的 javascript 数组。如何在我的控制器中处理这样的数组?

public ActionResult Index(THE_TYPE_TO_USE filter) {
}

我应该放THE_TYPE_TO_USE什么?我试过了objectdynamic但他们都产生了一个空过滤器。请注意,从客户端发送的过滤器也可能很复杂,如下所示:

$('#Solicitor').kendoComboBox({
    placeholder: "@T("Enter the partial Name or Primary ID of the Entity.").Text",
    dataTextField: "text",
    dataValueField: "id",
    autoBind: false,
    minLength: 3,
    filter: "startswith",
    dataSource: {
        serverFiltering: true,
        // Additional filters which span several levels deep
        filter: {
            filters: [
                {
                    filter: {
                        logic: "or",
                        filters: [
                            { field: "content-type", operator: "eq", value: "Company" },
                            { field: "content-type", operator: "eq", value: "Firm" }
                        ]
                    }
                }
            ]
        },
        transport: {
            read: {
                url: "@Url.Action("Index", "EntitiesAdmin", new { area = "BizNet.Entity"})",
                dataType: "json"
            }
        }
    }
});

这将产生以下解析的查询字符串

filter[filters][0][filter][logic]:or
filter[filters][0][filter][filters][0][field]:content-type
filter[filters][0][filter][filters][0][operator]:eq
filter[filters][0][filter][filters][0][value]:Company
filter[filters][0][filter][filters][1][field]:content-type
filter[filters][0][filter][filters][1][operator]:eq
filter[filters][0][filter][filters][1][value]:Firm
filter[logic]:and
filter[filters][1][value]:CAR
filter[filters][1][field]:text
filter[filters][1][operator]:startswith
filter[filters][1][ignoreCase]:true

如您所见,数组可以有好几级深。所以我的问题是,我应该在THE_TYPE_TO_USE上面放什么可以处理任意深度的复杂过滤器?这只能通过 ASP.NET MVC Wrappers 来完成吗?如果是这样,怎么做?

4

1 回答 1

1

嗨,伙计们,如果您仍在寻找解决方案,那么下面是代码:

   1:         public JsonResult GetCountries(string text)
   2:         {
   3:             var countries = new List<SelectListItem>
   4:                             {
   5:                                 new SelectListItem{Text ="Bulgaria",
   6:                                                    Value="1"},
   7:                                 new SelectListItem{Text ="United States of America",
   8:                                                    Value="2"},
   9:                                 new SelectListItem{Text ="India",
  10:                                                    Value="3"},
  11:                                 new SelectListItem{Text ="Australia",
  12:                                                    Value="4"},
  13:                                 new SelectListItem{Text ="United Kingdom",
  14:                                                    Value="5"},
  15:                             };
  16:             if (!string.IsNullOrEmpty(text))
  17:             {
  18:                 countries = (from item in countries
  19:                              where item.Text.StartsWith(text)
  20:                              select item).ToList();
  21:             }
  22:             return Json(countries, JsonRequestBehavior.AllowGet);
  23:         }

组合框初始化。代码

   1:         @(
   2:             Html.Kendo().ComboBox().Name("kcombobox")
   3:             .HtmlAttributes(new { style = "width:250px" })
   4:             .Placeholder("Select a value...")
   5:             .DataTextField("Text")
   6:             .DataValueField("Value")
   7:             .Filter(FilterType.StartsWith)
   8:             .DataSource(source =>
   9:             {
  10:                 source.Read(read =>
  11:                 {
  12:                     read.Action("GetCountries", "Home");
  13:                 }).ServerFiltering(true);
  14:             })
  15:        )

如果您希望处理额外的过滤器以及用于组合框的过滤器,那么您将需要DataSourceRequest自己处理过滤器的其余部分,或者您可以通过 Javascript 函数在客户端进行过滤,方法是在 DataSource 上应用过滤器事件数据绑定事件。

于 2014-07-25T08:45:02.863 回答