Gurmeet
Gurmeet

Reputation: 3314

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:enter image description here

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.

Tried with model approach

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:

enter image description here

Upvotes: 3

Views: 1536

Answers (3)

Guillermo Gutiérrez
Guillermo Gutiérrez

Reputation: 17799

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.

Upvotes: 1

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

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)

Upvotes: 2

Guillermo Guti&#233;rrez
Guillermo Guti&#233;rrez

Reputation: 17799

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/

Upvotes: 1

Related Questions