PASSING ARRAY FROM AJAX TO CONTROLLER

Total Post:88

Points:616
C# 
C#
 1816  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. 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
          });

      Modified On Apr-04-2018 01:51:24 AM

Answer

NEWSLETTER

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