I am having issue: I am sending simple date and consumerid to my handler method through Ajax, but in the method, I’m always getting null. I think the issue is with the date.
Can anyone help me?
Client-side code:
consumer2.ConsumerId
var v1 = new Date();
var date = v1.getDate()+ "/" + (v1.getMonth() + 1) + "/" + v1.getFullYear();
// consumer2.datelocal = date;
console.log(consumer2);
console.log("------");
console.log(consumer);
$.ajax({
type: 'POST',
url: getRootPath() + '/AjaxHandlers/Details?handler=UpdateConsumer2',
headers: {
'RequestVerificationToken': token
},
data: JSON.stringify(consumer2),
// data: JSON.stringify({ Consumer: consumer2, UpdateConsumer: PartyUpdate, ChannelOfContactibility: "MAIL" }),
// data: JSON.stringify( consumer ),
contentType: 'application/json; charset=utf-8',
Web service method:
public class Consumer2
{
public Nullable<DateTime> datelocal { get; set; }
public int ConsumerId { get; set; }
}
public IActionResult OnPostUpdateConsumer2([FromBody] Consumer2 consumer)
{
return null;
}
2
The Date object in JavaScript when formatted as dd/mm/yyyy
might not be automatically recognized by .NET as a valid DateTime. To ensure proper parsing, use the yyyy-MM-dd
format:
var consumer2 = {
ConsumerId: 123,
datelocal: ""
};
var v1 = new Date();
var date = v1.getFullYear() + "-" + (v1.getMonth() + 1) + "-" + v1.getDate();
consumer2.datelocal = date;
4