我必须传递这样的过滤器数组:
脚本代码:
return { CustomFilter: options.filter.filters };
+++++++++++++++++++++++++++++++++++++++++++++++ +
来自萤火虫:
CustomFilter[0][field] ItemName
CustomFilter[0][operator] startswith
CustomFilter[0][value] testing Value
CustomFilter[1][field] BrandName
CustomFilter[1][operator] startswith
CustomFilter[1][value] testing Value 1
发布值为:
但是我无法在控制器方面收到这些内容。
我试过这样:
public ActionResult ReadOperation( string[][] CustomFilter)
也是这样的:
public ActionResult ReadOperation( Filter[] CustomFilter)
public class Filter
{
public string field { get; set; }
public string @operator { get; set; }
public string value { get; set; }
}
但没有奏效。请建议。
谢谢。
使用Json反序列化找到解决方案
脚本代码更改为:
return { CustomFilter: JSON.stringify(CustomFilter) };
控制器代码更改为:
using Newtonsoft.Json;
public ActionResult ReadOperation(MyViewModel model)
{
var filters = JsonConvert.DeserializeObject(model.CustomFilter, typeof(CustomFilter[]));
}
public class MyViewModel
{
public string Filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
public class CustomFilter
{
public string field { get; set; }
public string @operator { get; set; }
public string value { get; set; }
}
控制器中的结果视图:
答案 0 :(得分:2)
模型结构看起来像是一个错误。
public class MyViewModel
{
public Filter[] CustomFilter { get; set; }
public string Filter { get; set; }
public string Group { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
public int Sort { get; set; }
}
尝试将此类型用于模型绑定。
public ActionResult ReadOperation(MyViewModel model)
答案 1 :(得分:1)
在帖子中,尝试发送数据:
CustomFilter[0].Field ItemName
CustomFilter[0].Operator startswith
CustomFilter[0].Value testing Value
CustomFilter[1].Field BrandName
CustomFilter[1].Operator startswith
CustomFilter[1].Value testing Value 1
在控制器上:
public ActionResult ReadOperation(Filter[] CustomFilter)
将Filter
类定义为:
public class Filter
{
public string Field { set; get; }
public string Operator { set; get; }
public string Value { set; get; }
}
(小心大写字母)。
或者如果你想使用模型方法,正如Ufuk建议的那样,并且具有相同的Filter
类:
型号:
public class MyViewModel
{
public Filter[] CustomerFilter { get; set; }
public string Filter { get; set; }
public string Group { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
public int Sort { get; set; }
}
POST中的参数:
CustomFilter[0].Field ItemName
CustomFilter[0].Operator startswith
CustomFilter[0].Value testing Value
CustomFilter[1].Field BrandName
CustomFilter[1].Operator startswith
CustomFilter[1].Value testing Value 1
Filter ItemName~startswith~'12'~and~BrandName~startswith~'123'
Group
Page 1
PageSize 15
Sort
控制器
public ActionResult ReadOperation(MyViewModel model)
请参阅此链接:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
答案 2 :(得分:1)
如果您使用的是ASP.NET MVC 4,并且无法更改参数名称,则可以通过以下方式定义自定义模型:
public class MyViewModel
{
public Dictionary<string,string>[] CustomerFilter { get; set; }
public string filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
然后,在控制器上:
public ActionResult ReadOperation(MyViewModel model){ ... }
网格生成的参数中使用的符号似乎是dictionaries。但是,没有尝试过字典集合。