3

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

Gurmeet
  • 3,094
  • 4
  • 19
  • 43

3 Answers3

2

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)
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • Thanks @Ufuk .i have tried as you told but still it is showing CustomFilter as null. – Gurmeet Jan 09 '14 at 05:56
  • Yes @Ufuk i noticed that, I had already tried with correct property name "CustomFilter". Again properties values were null. i had attached a screen shot from controller at bottom of Question, you can check that. – Gurmeet Jan 09 '14 at 07:19
  • @Gerry Can you show your form HTML. It looks like model binding won't work with current format. – Ufuk Hacıoğulları Jan 09 '14 at 07:27
  • 1
    i have done this with Newtonsoft.json Object deserialization. returned this from { CustomFilter: JSON.stringify(CustomFilter) }; and deserialized in controller. JsonConvert.DeserializeObject(model.CustomFilter, typeof(Filter[])); Thanks @Ufuk for your model approach:) – Gurmeet Jan 09 '14 at 09:35
1

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/

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
  • Thanks @Guillermo, But i can pass values to controller as you told because these are sent by kendo Grid itself. Other wise i have to modify filter request using script(dont want to do that). – Gurmeet Jan 09 '14 at 04:51
  • I see, so that is the default format of Kendo Grid parameters. The only way then would be to make a custom model binder. – Guillermo Gutiérrez Jan 09 '14 at 13:41
1

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.

Community
  • 1
  • 1
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
  • 1
    Thanks for your suggestion. Although i had already done this with json De serialization. But i tried your way also and it worked for me. Thanks :) – Gurmeet Jan 10 '14 at 04:49