简体   繁体   中英

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:

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

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:

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:

  • 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:

     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/

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM