I have the following JSON being sent to an MVC controller:
{
"CId": 374,
"CultureId": 1,
"VGS": null,
"DisplayOrder": 1
}
I am using JSON.Net to convert this to a dynamic object and later assigning the properties into an entity:
public partial class FooEntity
{
public short DisplayOrder { get; set; }
public Nullable<short> VGS { get; set; }
public short CId { get; set; }
public short CultureId { get; set; }
}
Notice that the VGS property we are assigning into is a nullable short, however when trying to create a new instance of the entity and assign the values, I get an error when trying to assign the VGS:
dynamic data = JsonConvert.DeserializeObject(payload);
var foo = new FooEntity();
foo.CId = data.CId;
foo.CultureId = data.CultureId;
foo.VGS = data.VGS; // Errors here
foo.DisplayOrder = data.DisplayOrder;
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
As far as I can tell, the json is correct to deserialize into a null value, and since I am assigning into a nullable value, I'm not sure what is causing the error?
VGSis being deserialized asobject, and that's why you can't assign it to aNullable<sort>short?overNullable<short>