我试图在服务器端处理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
放入什么?我已尝试object
和dynamic
,但他们都生成了空过滤器。请注意,从客户端发送的过滤器可能很复杂,如下所示:
$('#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完成吗?如果是这样,怎么样?
答案 0 :(得分: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: }
Combobox Init。代码
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 ,它可以通过自己的OR处理剩余的过滤器,或者您可以在客户端上进行过滤通过在事件DataBound事件中对DataSource应用过滤器,通过Javascript函数。