Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

passing array from ajax to controller

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();
        }

Upvotes: 0

Views: 281

Answers (2)

abc123
abc123

Reputation: 262

Your code should be like this

     $.ajax({
          url: "Home/userDetails",
          data: {
              "UserDetailsParam":JSON.stringify(userDetails)//change this
          },
          responseType: "json",
          success: successfn,
          error: errorfn
      });

Upvotes: 1

Sridhar R
Sridhar R

Reputation: 20418

Try with this

var userDetails = {"age":'12',"Name":'Vignesh',"lastName":'s'};

Upvotes: 0

Related Questions