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();
}
tereško
58.5k26 gold badges100 silver badges151 bronze badges
asked Dec 30, 2013 at 5:48
2 Answers 2
Your code should be like this
$.ajax({
url: "Home/userDetails",
data: {
"UserDetailsParam":JSON.stringify(userDetails)//change this
},
responseType: "json",
success: successfn,
error: errorfn
});
4 Comments
Vignesh Subramanian
Thanks ! :) In the controller I am not able to get an array if i deserialize the paramaeter passed!
abc123
i am also serializing parameters for sending like the way you are doing and by deserializing i was not able to get parameter as an array.
Vignesh Subramanian
are you using the same code like this var array = new JavaScriptSerializer().DeserializeObject(UserDetailsParam);
abc123
i am using this Newtonsoft.Json dll for serializing and deserializing
Try with this
var userDetails = {"age":'12',"Name":'Vignesh',"lastName":'s'};
answered Dec 30, 2013 at 5:53
Comments
default
UserDetailsParam: JSON.stringify(userDetails)