In my model class, I have the following:
public class DataModel
{
public bool Display { get; set; }
}
After deserializing a FormData object to a JSON object, I am posting the value from a checkbox like this:
this.FormToJSON = form => {
const json = {};
new FormData(form).forEach((value, key) => {
// Ignore the Anti-Forgery Token
if (key !== "__RequestVerificationToken") {
json[key] = value;
}
});
return json;
}
The handler in the page is as follows:
public IActionResult OnPostSave([FromBody] DataModel model)
{
// Do some stuff
}
Even though the Display property is bound to a checkbox input, attempting to deserialize its value of "true" results in the model parameter being null.
A simple fix for this would be:
json[key] = JSON.parse(value);
However, submitting a text field with a value of "true" converts it to a boolean, which is not desired.
Is there a more effective way to handle this? I prefer using AJAX for all requests to handle responses on the front end without relying on jQuery.