1

Iv searched related questions, but still not able to get this to work in my situation. I am working in ASP.NET MVC.

I have a report that has 3 filters. Only 1 value can be selected for Filters 1 and 2. My 3rd filter is a Multi Select filter. I load an array with all the values selected from the Multi Select Filter, then I want to pass all 3 Filters to my controller action. Filters 1 and 2 work correctly, but my Multi Select Filter is not being passed, and shows as Null in my Actions parameter.

Below is my JavaScript and Ajax call

$('#applyFilter').click(function () {

            var options = new Array();
            //Fill array with values from Multi Select Filter
            $('#treeFilter > option:selected').each(
                function (i) {
                    options[i] = $(this).text();
                })


            var filter1 =  $('#LOCFilterDropdown').val();
            var filter2 = $('#ESNFilterDropdown').val();

            $.ajax({
                type: "GET",
                url: "/Home/FilterReport",

                data: {
                    filterOne: filter1,
                    filterTwo: filter2,
                    multiselectFilter: options
                },
                success: function (result) {
                    $('#reportTable').html(result);
                }
            });
        });

The receiving Action

[HttpGet]
public ActionResult FilterReport(string filterOne,string filterTwo, int? page, List<string> multiselectFilter)
{
     //filterOne contains correct value
     //filterTwo contsins correct value
     //multiSelectFilter contains null

     //returns partial view

}

Can anyone tell me why my

List<string> multiselectFilter
//Iv also tried string[] multiselectFilter, but this didnt change anything

is now receiving the array from the ajax call?

Reeggiie
  • 782
  • 7
  • 16
  • 36
  • what does your html look like – pparas May 23 '16 at 20:42
  • @pparas The html is fine I believe. I tested the array with Console.Log() to view the contents of the array. The array has the correct values inside it. – Reeggiie May 23 '16 at 20:43
  • @pparas This is how I create the filter list, had to remove the @ in the razor sytax to post this comment. – Reeggiie May 23 '16 at 20:46
  • Hmm... Need a `JSON.stringify()` somewhere? Too much for a `GET` method? Is the data present in the request when it gets to the controller but not getting mapped correctly? Just thinking out loud... – James R. May 23 '16 at 20:54

3 Answers3

1

You could also try to add these in your ajax call add:

dataType: "json",
traditional: true

If the options array can get big, I would use POST so that you avoid reaching the url max length limit.

Petre Ionescu
  • 174
  • 1
  • 8
0

You are trying to send a complex object with GET method. The reason this is failing is that GET method can't have a body and all the values are being encoded into the URL.

Similar Question

Complex type is getting null in a ApiController parameter

I Would just join the Multiselectfilter in client and split them in server side

 multiselectFilter: options.join(',')
[HttpGet]
    public ActionResult FilterReport(string filterOne, string filterTwo, int? page, string multiselectFilter)
    {
        string[] array = multiselectFilter.Split(',');

        //returns partial view

    }
Community
  • 1
  • 1
Richard Vinoth
  • 280
  • 3
  • 12
  • Perfect, thank you. I also tried changing 'GET' to 'POST' based on your answer, and that works also. – Reeggiie May 23 '16 at 21:14
0

I have created a fiddle here. I think you might have an issue with the serialization. Although I am not a hundred percent sure. Here is the code to serialize options = JSON.stringify(options); , Like the previous answer has said.

pparas
  • 549
  • 4
  • 13