如何在MVC控制器中访问Javascript多维数组 [英] how to access Javascript multidimensional array in MVC controller

查看:45
本文介绍了如何在MVC控制器中访问Javascript多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过这样的过滤器数组:

I have to pass array of filters like this:

脚本代码:

return { CustomFilter: options.filter.filters };

+++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++

来自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

发布的值为:

但是我无法在Controller端收到这些信息.

But i am unable to received these on Controller side.

我尝试过这样:

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; }
}

但是没有用.请提出建议.

But didn't work. Please Suggest.

谢谢.

发现具有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; }
}

控制器中的结果视图:

推荐答案

如果您使用的是ASP.NET MVC 4,并且无法更改参数名称,也许您可​​以通过以下方式定义自定义模型:

If you are using ASP.NET MVC 4, and cannot change the parameter names, maybe you can define a custom model in this way:

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){ ... }

您的网格生成的参数中使用的符号似乎是针对词典的.不过,还没有尝试过词典的收集.

It seems that the notation used in the parameters generated by your grid is for dictionaries. Haven't tried a collection of Dictionaries, though.

这篇关于如何在MVC控制器中访问Javascript多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆