PASSING ARRAY FROM AJAX TO CONTROLLER

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
C# 
C#
 1523  View(s)
Ratings:
Rate this:

I am trying to pass an array variable from ajax to controller, but I am not getting the values in controller

below is my code

AJAX

 function userDetailsClass() {

          var userDetails = {};

          userDetails.age = 12;

          userDetails.Name = "Vignesh";

          userDetails.lastName = "s";

          debugger;

          $.ajax({

              url: "Home/userDetails",

              data: JSON.stringify({

                  UserDetailsParam: userDetails

              }),

              responseType: "json",

              success: successfn,

              error: errorfn

          });

          function successfn(result) {

          };

          function errorfn(result) {

          };

      }

Controller

public ActionResult userDetails( string UserDetailsParam){

            return View();

        }

I also tried

public ActionResult userDetails( string[] UserDetailsParam){

            return View();

        }

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Passing array from ajax to controller

    Hi Jacob,

    Your code should be like this

         $.ajax({

              url: "Home/userDetails",

              data: {

                  "UserDetailsParam":JSON.stringify(userDetails)//change this

              },

              responseType: "json",

              success: successfn,

              error: errorfn

          });

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!