[英]how to access Javascript multidimensional array in MVC controller
I have to pass array of filters like this: 我必须传递这样的过滤器数组:
Script Code: 脚本代码:
return { CustomFilter: options.filter.filters };
++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++
From Firebug: 来自Firebug:
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
Posted values are: 发布的值是:
But i am unable to received these on Controller side. 但我无法在控制器方面收到这些信息。
i tried like this: 我试过这样的:
public ActionResult ReadOperation( string[][] CustomFilter)
Also like this: 也像这样:
public ActionResult ReadOperation( Filter[] CustomFilter)
public class Filter
{
public string field { get; set; }
public string @operator { get; set; }
public string value { get; set; }
}
But didn't work. 但没有奏效。 Please Suggest.
请建议。
Thank you. 谢谢。
Solution Found with Json deserialization 使用Json反序列化找到解决方案
Script code changed to: 脚本代码更改为:
return { CustomFilter: JSON.stringify(CustomFilter) };
Controller Code changed to: 控制器代码更改为:
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; }
}
Result View in controller: 控制器中的结果视图:
It looks like an error with model structure. 它看起来像模型结构的错误。
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; }
}
Try to use this type for model binding. 尝试将此类型用于模型绑定。
public ActionResult ReadOperation(MyViewModel model)
In the post, try to send the data as this: 在帖子中,尝试发送数据:
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
And at the controller: 在控制器:
public ActionResult ReadOperation(Filter[] CustomFilter)
Having a Filter
class defined as: 将
Filter
类定义为:
public class Filter
{
public string Field { set; get; }
public string Operator { set; get; }
public string Value { set; get; }
}
(Be careful with the capital letters). (小心大写字母)。
Or if you want to use the model approach, as Ufuk suggests, and having the same Filter
class: 或者如果你想使用模型方法,正如Ufuk建议的那样,并且具有相同的
Filter
类:
Model: 模型:
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; } }
Parameters in POST: 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
Controller 调节器
public ActionResult ReadOperation(MyViewModel model)
See this link: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ 请看这个链接: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
If you are using ASP.NET MVC 4, and cannot change the parameter names, maybe you can define a custom model in this way: 如果您使用的是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; }
}
Then, at the controller: 然后,在控制器:
public ActionResult ReadOperation(MyViewModel model){ ... }
It seems that the notation used in the parameters generated by your grid is for dictionaries . 似乎网格生成的参数中使用的符号用于字典 。 Haven't tried a collection of Dictionaries, though.
但是,没有尝试过字典集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.