javascript - Pass Data string using ajax to controller method -


i creating filter list. have several multi-select lists user can choose several options. problem using ajax return values , call controller method return json data displayed on website. problem doesn't work.

controller method:

[httppost] public actionresult filterdata(string a, string b, string c, string d){   //do strings e.g. filter data.   return json(result, jsonrequestbehavior.allowget); } 

ajax method 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 can see right values posted. , know if manually select string via url link correct filtered data. however, data not seem method , output data without being filtered using postes values.

the problem data posted not being pciked or sent method filterdata, returns data should if there no filter options selected. know how can fix code can data posted ajax send data method filterdata parameters e.g. string a, string b, string c , string d.

update: when restated problem, here line had issue (you using json.stringify on data value):

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

you converting data 1 json string, not mvc expects.

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

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

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

so 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 post request (although doesn't break anything).


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -