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

4
  • Does your code compile at all? Commented Jan 8, 2014 at 12:53
  • Yes,it is also showing count 2 as i am passing 2 filters, but values of field,operator and value is showing null. Commented Jan 8, 2014 at 13:01
  • Can you post the raw JSON? Firebug should show what is posted. Commented Jan 8, 2014 at 13:07
  • I have added Firebug image of posted values Commented Jan 8, 2014 at 13:18

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Ufuk .i have tried as you told but still it is showing CustomFilter as null.
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.
@Gerry Can you show your form HTML. It looks like model binding won't work with current format.
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:)
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.

1 Comment

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 :)
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/

2 Comments

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).
I see, so that is the default format of Kendo Grid parameters. The only way then would be to make a custom model binder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.