Skip to content Skip to sidebar Skip to footer

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

Solution 1:

It looks like an error with model structure.

publicclassMyViewModel
{
    public Filter[] CustomFilter { get; set; }
    publicstring Filter { get; set; }
    publicstring Group { get; set; }
    publicint Page { get; set; }
    publicint PageSize { get; set; }
    publicint Sort { get; set; }
}

Try to use this type for model binding.

public ActionResult ReadOperation(MyViewModel model)

Solution 2:

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

publicclassMyViewModel
{
    public Dictionary<string,string>[] CustomerFilter { get; set; }
    publicstring filter { get; set; }
    publicstringgroup { get; set; }
    publicint page { get; set; }
    publicint pageSize { get; set; }
    publicint 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.

Solution 3:

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:

publicclassFilter
{
    publicstring Field { set; get; }
    publicstring Operator { set; get; }
    publicstring 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:

    publicclassMyViewModel
    {
        public Filter[] CustomerFilter { get; set; }
        publicstring Filter { get; set; }
        publicstring Group { get; set; }
        publicint Page { get; set; }
        publicint PageSize { get; set; }
        publicint 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 Value1Filter                     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/

Post a Comment for "How To Access Javascript Multidimensional Array In Mvc Controller"