How to pass an object to controller using ajax call
ajax, asp.net-mvc, jquery
Solution
Try this:- You are passing an array of objects so you should do HTTPPost rather than HttpGet (this will work for array of primitive types say list of int, strgin etc) sending it through query string(Remember the limit for query string). Try this with HTTPPost
$.ajax({
type: "POST",
url:"Home/Position",
data: JSON.stringify({
array: positionarray
}),
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
[HTTPPost]
public void Position(YourClass[] array){...
Problem
I want to pass an object to controller and retrieve the values in the controller. I have defined like: Html code: ``` var positionarray = []; ``` Javascript: ``` $("#button").live('click',function(){ positionarray.push({ id: sessionStorage.getItem('id'), value: $("#input").val() }); }); // on save button click $.ajax({ type: "GET", url:"/Bugs/Position", data: { array:positionarray }, cache: false, dataType: "json", contentType: "application/json; charset=utf-8", success: function (json) { } }); ``` But i am not able to retrieve the values in the controller. It is getting null.