Skip to content

How to pass multiple values fm jquery ajax to C# web methd?

Arun B

Asked Sep 2, 2013, 9:31 PM EDT8 answers
Share
LinkedInX
Hi Frds,


 i hav a problm in passing multiple values frm  jquery to c# web method





i'm using chosen plug in any 1 help me














6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    Hi Arun,

    Please share your code.
    0
  • 0
     <script type="text/javascript">
                $(document).ready(function () {
                    $("#btn_advancedSearch").click(function () {
                        var test = [];
                        var j = 0;
                        $('#Listbox_Marital :selected').each(function (i, selected) {
                            test[j] = $(selected).val();
                            j++;
                        });

     $.ajax({
                            url: "Default.aspx?test=" + test + "",
                            //url: "Default.aspx/MaritalStatus",
                            //data: JSON.stringify(f),
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",

      });
      });
    0
  • 0
    c# coding [WebMethod(EnableSession = true)] public static string MaritalStatus(List value) { string final = value[0].value.ToString(); HttpContext.Current.Session["maritalstatus"] = final; return final; } [WebMethod(EnableSession = true)] public static string Religion(List Rvalue) { string final = Rvalue[0].rvalue.ToString(); HttpContext.Current.Session["Religion"] = final; return final; }
    0
  • Sunny  Sharma
    0
    ok.

    You just combine the multiple values with a comma as separator in your data param while ajax operation like:

    data:{maritalStatus:'Divorced,Never Married,etc,etc'}

    that's it.

    You can then split the values at server and process them, how's that?
    0
  • Sunny  Sharma
    0
    give me your aspx and code behind page
    0
  • Sunny  Sharma
    0
    I would like to keep it here only. The solution is that simple. When you're making AJAX call, concatenate all the values in a string that you want to pass and attach it with "data" parameter in AJAX call.

    At server side code, just get the data and split it, like:

    string[] AllSelectedOptions = receivedString.split(",");

    There you go.

    I hope it makes sense.

    Happy Coding :)
    0
  • 2 short replies
    • 0
      can u give me the code ????
      0
    • 0
      ur mail id plz
      0