My problem: I’m sending JSON data, but my JSON data is going “null”? I don’t understand why it’s going null.
I read to all documentation but I am new for jquery.
What am I missing?
I need to send JSON data for my controller.
Index.cshtml:
var UserData = {
"CustomerID": "99999",
"CustomerCode": "0",
"UserID":"127",
"StartDate": "02/09/2019",
"FinishDate": "03/08/2020",
"SuccesID": "1",
"Cash": "1",
"ProblemID": "1",
"PageNo":"1",
"DataNo":"1"
};
var userDataJson = JSON.stringify(UserData);
$.ajax({
type: "POST",
url: "/Problems/Search",
contentType: "application/json",
data: userDataJson,
success: function (msg) {
console.log(msg);
},
error: function (err) {
alert("search error");
}
});
Model:
public class ProblemsInput
{
public string CustomerID { get; set; }
public string CustomerCode { get; set; }
public string UserID { get; set; }
public string StartDate { get; set; }
public string FinishDate { get; set; }
public string SuccesID { get; set; }
public string Cash { get; set; }
public string ProblemID { get; set; }
public string PageNo { get; set; }
public string DataNo { get; set; }
}
Controller:
[HttpPost]
public IActionResult Search(ProblemsInput problemsinput)
{
return View();
}
3
Answers
Just apply
FromBody
attribute to theproblemsinput
parameter:Dude. This problem is your UserData. Try this code:
If this doesnt work. Use [FromBody] attribute in Controller an try again. I hope this help you.
You just need to add
instead of
data: userDataJson,
This will solve the problem.
Ajax call will be