Reputation: 1137
I am trying to send the null data by JQuery AJAX. However, I end up getting an error message which says "Bad Request". I have tried using the word "null" to send to the database, however, this has not worked. The database does allow for nulls. I can manually add data directly into the database while inputting no data in the null columns.
$.ajax({
method: 'POST',
url: 'http://localhost:58371/api/ScheduleDateAndTime',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"startDateAndTime": startTotalTimeAndDate,
"endDateAndTime": endTotalTimeAndDate,
"studentId": studentIdSelected,
"staffId": staffIdSelected
}),
success: function (data) {
alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " + datePickerValue + " starting at " + startTime + " and ending at " + endTime);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError + " have you checked that you have selected a date in the calander?\n Please ensure all fields have been correctly entered");
}
});
I want to be able to send null data on the "studentId" column in the database. "studentId" is a variable that gets its data from a drop-down list, where one of the available options is "none", which has "null" as its id.
Upvotes: 2
Views: 475
Reputation: 406
Dont send the value that should be null
var DataToSend = {};
if(startTotalTimeAndDate != undefined)
DataToSend.startDateAndTime= startTotalTimeAndDate;
if(endTotalTimeAndDate!= undefined)
DataToSend.endDateAndTime= endTotalTimeAndDate;
if(studentIdSelected!= undefined)
DataToSend.studentId= studentIdSelected;
if(staffIdSelected!= undefined)
DataToSend.staffId= staffIdSelected;
$.ajax({
method: 'POST',
url: 'http://localhost:58371/api/ScheduleDateAndTime',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(DataToSend),
success: function (data) {
alert("You have assigned staff member " + staffSelected + " to student " + studentSelected + " on " + datePickerValue + " starting at " + startTime + " and ending at " + endTime);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError + " have you checked that you have selected a date in the calander?\n Please ensure all fields have been correctly entered");
}
});
Upvotes: 1
Reputation: 1137
The ASP.NET DTO model did not allow for nulls, once this was changed to the following I was then able to upload nulls
public int? studentId { get; set; }
Upvotes: 3