Reputation: 1076
I have an object (mockup below) I've populated client-side with JavaScript and I need to post it to my server. There's a corresponding type of object defined in a class server-side that I'm expecting to receive.
When I attempt to post the object, everything posts fine except for a single property. This property is defined server-side as a List<KeyValuePair<Guid,Bar>>
. The Bar
doesn't seem to be the issue, as I can post that just fine.
Client-side, I've attempted to reformat the corresponding property on the JavaScript object in a few ways (as an array of object pairs, as literal properties on the object, etc.), but it's always an empty list when it arrives at the server.
I'm guessing this is a syntax issue, but I'm having trouble figuring out what the proper syntax is, and I'm seeing a lot of conflicting examples online. I'm hoping someone can speak from experience as to how this should be posted.
//C# Class MockUp
public class Foo
{
public Guid FooId {get;set;}
public string Description {get;set;}
public bool Inactive {get;set;}
public List<KeyValuePair<Guid,Bar>> FooBar {get;set;} //This comes over as null regardless of how I arrange the object client-side.
}
//TypeScript Class MockUp
class Foo {
fooId: string;
description: string;
inactive: boolean;
fooBar: Array<KeyValuePair>;
}
class KeyValuePair {
key: string;
value: Bar
}
class Bar {
//...Implementation here mathces Bar on the server
}
Upvotes: 0
Views: 214
Reputation: 3231
Will work if you create a dto for foobar
public class FooDto
{
public Guid FooId {get;set;}
public string Description {get;set;}
public bool Inactive {get;set;}
public List<FooBarDto> FooBar {get;set;}
}
public class FooBarDto
{
public Guid Key {get;set;}
public Bar Value {get;set;}
}
The reason why KeyValuePair
will not work in this case is because the properties in KeyValuePair
are read only, take a look here at the msdn docs you can see that the Key and Value proerties only have getters
Upvotes: 1