I tried Different forms of calls, same names, with stringify, without stringify.... but nothing works.
my html
<input type="text" id="txtName"/>
<input type="button" id="btnGet" value="Get Current Time"/>
<input type="text" id="txtresponse"/>
my jscript
$(function () {
$("#btnGet").click(function () {
$.ajax({
type: "POST",
url: "/Home/AjaxMethod",
data: '{name: "' + $("#txtName").val() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
//alert(response);
$("#txtresponse").val(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
//alert(response.responseText);
}
});
});
});
my controller
[HttpPost]
public ContentResult AjaxMethod(string name)
{
string currentDateTime = string.Format("Hello {0}.\nCurrent DateTime: {1}",
name, DateTime.Now.ToString());
return Content(currentDateTime);
}
Here, the "AjaxMethod" controller always receives null as the value of the "name" parameter.
My Version is .Net 2022 and .Net 6
Thank you very much for your help