2

I am creating a filter list. I have a several multi-select lists where the user can choose several options. The problem is I am using ajax to return the values and call a controller method which will return a Json data which will then be displayed on the website. But the problem is it doesn't work.

Controller Method:

[HttpPost]
public ActionResult FilterData(string a, string b, string c, string d){
  //Do Something with the strings e.g. filter the data.
  return Json(result, JsonRequestBehavior.AllowGet);
}

Ajax Method for getting filtered data:

$(document).ready(function() {
$("#submitBTN").click(function() {
var ab = $('#selectedID1').val();
var bc = $('#selectedID2').val();
var cd = $('#selectedID3').val();
var de = $('#selectedID4').val();

$.ajax({
       contentType: "application/json; charset=utf-8",
       url:"/Controller/FilterData",
       type: "POST",
       data: JSON.stringify({a: ab, b: bc, c: cd, d:de }),
       success: function(result){

       } 
       error: function(){}
     });
       return false;
      });
   });

Using firebug I can see the right values are posted. And I know that if I manually select string a via the url link it get the correct filtered data. However, this data does not seem to get into the method and output all the data without being filtered using the postes values.

The problem is that the Data Posted is not being pciked up or sent to the method FilterData, so it returns all the data as it should do if there are no filter options selected. I would like to know how I can fix the code so I can get the data posted by ajax to send that data to method FilterData parameters e.g. string a, string b, string c and string d.

3
  • At least try to avoid typos in your example(s). Closing brackets and quotes, etc. Commented Aug 10, 2013 at 10:52
  • There is nothing returned in the ajax error? Commented Aug 10, 2013 at 10:57
  • @user1938460: after few edits... now you're only missing comma after success. Commented Aug 10, 2013 at 11:05

2 Answers 2

1

Update: Now when you restated problem, here is the line that had issue (you were using JSON.stringify on data value):

data: { a: ab, b: bc, c: cd, d: de }

You were converting data into 1 JSON string, which is not what MVC expects.

"{"a":1,"b":2,"c":3,"d":4}"

Instead it expects key value pairs (normal post-back format):

a=1&b=2&c=3&d=4

So now full example without typos:

$(document).ready(function () {
    $("#submitBTN").click(function(){
        var ab = $('#selectedID1').val();
        var bc = $('#selectedID2').val();
        var cd = $('#selectedID3').val();
        var de = $('#selectedID4').val();

        $.ajax({
            url: '/MyController/FilterData',
            type: 'POST',
            data: { a: ab, b: bc, c: cd, d: de },
            success: function(result) {

            },
            error: function() {
            }
        });
    });
});

You don't need JsonRequestBehavior.AllowGet if it's just POST request (although it doesn't break anything).

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

15 Comments

changing the syntax doesnt make a difference. There is no error in the syntax. Its just not passing the values through to the ActionResult Method parameters. So its just returning all the data.
Well, it passed data when I use your example above. Did you forget to tell us something? Is your environment different then example you gave? Give me exact URL where AJAX call sends data.
Hi Nenad using breakpoints and the intellisense the values of the strings in the method parameters are null, therefore it will return all the data. The exact URL is url 'Project/FilterData'
"data:..." line in your code calls JSON.stringify. Remove it!
Oh, thanks. But it did not work, got this error: Invalid JSON primitive: a,
|
0

try with:

url:'/Controller/FilterData',

2 Comments

This was just a typo in the OP sample, this is not the solution.
Sorry forgot to add the "". But its doesnt make a difference. as the data is being posted and the method is called. But the json being returned is not filtered, as it doesnt get the posted data.

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.