When sending complex javascript objects to the Web API, I noticed on a video that a typed object is used as a parameter, but it is not clear on the steps needed to take place to unpack the data and map fields correctly.
Should a C# class be create that matches the javascript fields exactly? Since javascript objects of a specific type are not always created equal will this throw an error on the server side?
Example:
Two javascript objects of the same type.
function Person(){
this.name='';
this.habbits='';
}
var person = new Person();
person.name='mark'
person.habbits='girls'
//
var personTwo=new Person();
personTwo.name='jane';
personTwo.habbits='men';
personTwo.car='red';
Notice a .car property was added dynamically
What is the best way to approach this type of situation? Just create a C# class with all the possiable properties that could be passed to the WEB API?
erikkl2000
Member
257 Points
234 Posts
Web API & Javascript Complex Objects
May 20, 2012 04:24 PM|LINK
When sending complex javascript objects to the Web API, I noticed on a video that a typed object is used as a parameter, but it is not clear on the steps needed to take place to unpack the data and map fields correctly.
Should a C# class be create that matches the javascript fields exactly? Since javascript objects of a specific type are not always created equal will this throw an error on the server side?
Example:
Two javascript objects of the same type.
function Person(){ this.name=''; this.habbits=''; } var person = new Person(); person.name='mark' person.habbits='girls' // var personTwo=new Person(); personTwo.name='jane'; personTwo.habbits='men'; personTwo.car='red';Notice a .car property was added dynamically
What is the best way to approach this type of situation? Just create a C# class with all the possiable properties that could be passed to the WEB API?
Thanks!
javascript