0

I post data with jQuery but I have a problem with array data. The values supplied to the MVC controller are always null.

This is my JavaScript code:

 var FilterCategory = $('input:checkbox:checked').map(function () {
                return this.value;
            }).get();
 var posting = $.post(url, { cursorid: lastid, CatFilter: FilterCategory });

The form data from the network:

cursorid:5434cdc84ba4dd0c40396851
Filter[]:1
Filter[]:3
Filter[]:4

Here's the C# side:

public ActionResult GetDataTweets(string cursorid,string[] CatFilter)
    {
       bla bla
    }

cursorid has a value, but CatFilter is null.

What do I need to do to have the correct value supplied to CatFilter?

0

1 Answer 1

1

This code here:

var posting = $.post(url, { cursorid: lastid, CatFilter: FilterCategory });

From MVC's perspective it's sending a class, so you need a reciprocal C# class to package the data. Create a class to handle that data, for example:

I am assuming your CatFilter array is a string array:

public class Data{
  public string cursorid { get; set;}
  public string[] CatFilter {get; set;}
}

Then change your controller:

public ActionResult GetDataTweets(Data data)
{
   var s1 = data.cursorid;
   var s2 = data.CatFilter[0];
}

if CatFilter is a class then you'd need to create a CatFilter class to map to the javascript version.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.