How can I post multiple strings form jquery ajax to a C# controller action? I have this working with one string but not sure how to post more than one string to a method in c# taking two string parameters.
Jquery code:
var data = {
date: "s",
index: "sa"
}
$.ajax({
url: "/Home/PersistTimeOfDay",
type: 'post',
data: JSON.stringify(data),
contentType: 'application/json',
success: function(response) {
alert(response);
},
error: function(jqXHR, textStatus, errorThrown){
alert(jqXHR.status);
}
})
C# code:
[Microsoft.AspNetCore.Mvc.HttpPost]
public IActionResult PersistTimeOfDay([FromBody] string date, [FromBody] string index)
{
return Json("s");
}
Would love to finally achieve this! I have tried a number of combinations and either get one or all values null.
Don't apply [FromBody] to more than one parameter per action method." And as other community members shared, you can pass data via a custom model to achieve your requirement.