I’m creating an object in javascript with LocationId and id.
I have a class MyClass in C# with those two variables.
I have an ajax call which calls a method in my controller:
MyMethod(List<MyClass> myObject, int user_id)
I use:
traditional: true
in the ajax call.
The method gets called, myObject has the correct length, but LocationId and id are all 0. Why is this happening and how to solve it?
public class MyClass {
public int LocationId {get; set;}
public int id {get; set;}
}
Javascript:
var myArray = new Array();
var object = new {
LocationId: 1;
id: 2;
}
myArray.push(object);
Ajax:
$.ajax({
type: "GET",
url: "correctUrl",
traditional: true,
data: { user_id: 1, myObject: myArray}
});
Your Javascript code to create the object is invalid. Try this:
By the way, you could shorten this to: