In an asp.net5/mvc6 project I've created a page to edit documents from an Azure DocumentDB storage. I use jquery to post the data to a controller action.
A sample document from the database looks like this:
{
"key": "name1",
"value": 23
}
The property value can have different types (number, string, etc).
How can I keep this value dynamic when posting to the controller? If I create a class like this works fine:
public class Setting
{
{
[JsonProperty(PropertyName = "key")]
public string Key { get; set; }
[JsonProperty(PropertyName = "value")]
public string Value { get; set; }
}
}
In combination with a strongly typed parameter in the action:
public async Task<IActionResult> Update(string collectionName, List<Setting> settings)
But now the value is always of type string. I've tried to use JObject like this in the action:
public async Task<IActionResult> Update(string collectionName, List<JObject> settings)
But then deserialized parameter settings inside the action has 'null' values for all properties:
{ "key": null, "value": null}
//EDIT Sample of the JS code posting to the action:
var data = {
settings: [{ key: "test", value: 123}]
}
$.ajax({
url: 'controller/action',
type: 'post',
dataType: 'json',
data: data
});