After converting my application into .NET Core 3.1 , the FromBody json attribute is not working . So Ichanged into [FromBody] JsonElement model. After changing that how can I get the value from model into variable.
In Javascript
var model = {
Employees: EmpIds,
FromDate: $('#fromDate').val(),
ToDate: $('#toDate').val(),
Comment: $('#ment').val(),
}
var url = "/Attendance/BulkUpdate"
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(model),
success: function (response) {
if (response.success) {
ShowResultModalPopup(response.responseText);
} else {
// DoSomethingElse()
ShowErrorModalPopup(response.responseText);
}
},
failure: function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log(response.responseText);
}
});
}
In Controller
public IActionResult BulkUpdate([FromBody] JsonElement model)
{
DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate"));// How can I store the value from model variable into datetime
DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate"));
}
Thanks Pol
After converting my application into .NET Core 3.1 , the FromBody json attribute is not working . So Ichanged into [FromBody] JsonElement model. After changing that how can I get the value from model into variable.
In Javascript
var model = {
Employees: EmpIds,
FromDate: $('#fromDate').val(),
ToDate: $('#toDate').val(),
Comment: $('#ment').val(),
}
var url = "/Attendance/BulkUpdate"
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(model),
success: function (response) {
if (response.success) {
ShowResultModalPopup(response.responseText);
} else {
// DoSomethingElse()
ShowErrorModalPopup(response.responseText);
}
},
failure: function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log(response.responseText);
}
});
}
In Controller
public IActionResult BulkUpdate([FromBody] JsonElement model)
{
DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate"));// How can I store the value from model variable into datetime
DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate"));
}
Thanks Pol
Share Improve this question edited Apr 21, 2022 at 20:39 Ray Hayes 15k8 gold badges56 silver badges78 bronze badges asked Jun 11, 2021 at 11:19 Alan PauilAlan Pauil 2091 gold badge6 silver badges17 bronze badges2 Answers
Reset to default 3You could change like below:
DateTime fromdate = Convert.ToDateTime(model.GetProperty("FromDate").GetString());
DateTime todate = Convert.ToDateTime(model.GetProperty("ToDate").GetString());
You can use the built in methods to convert those properties to dates:
public IActionResult BulkUpdate([FromBody] JsonElement model)
{
var fromdate = model.GetProperty("FromDate").GetDateTime();
var todate = model.GetProperty("ToDate").GetDateTime();
}