There seems to be a missing piece in my code. Here's my controller method
public void SubmitSweep(int personID, int DD, int MM, int YYYY, int hh, int mm, int dealId)
Here's how I define my button
<button id="submit@(person.Id)" class="btn btn-secondary" OnClick="submitSweep(@(person.Id), @(person.sweepDay), @(person.sweepMonth), @(person.sweepYear), @(person.sweepHour), @(person.sweepMinutes), @(person.dealId))">Submit Sweep</button>
And here's my JS function
function submitSweep(thePersonID, sweepDay, sweepMonth, sweepYear, sweepHours, sweepMinutes, theDealId) {
$.ajax({
type: 'POST',
url: '@Url.Action("SubmitSweep", "Home")',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ personID: thePersonID, DD: sweepDay, MM: sweepMonth, YYYY: sweepYear, hh: sweepHours, mm: sweepMinutes, dealId: theDealId }),
success: function (data) {
alert("Success");
},
error: function (ob, errStr) {
alert("An error occured.Please try after sometime." + errStr + " " + ob.responseText);
}
});
}
The function is being executed and the arguments are being populated. However, after some investigation, I believe the issue lies within the AJAX data field. If I remove all arguments from the controller, the breakpoint is reached, leading me to believe the error is in the data line.
If I modify my JS function as follows
function submitSweep(thePersonID, sweepDay, sweepMonth, sweepYear, sweepHours, sweepMinutes, theDealId) {
var obj = {};
obj.personID = thePersonID;
obj.DD = sweepDay;
obj.MM = sweepMonth;
obj.YYYY = sweepYear;
obj.hh = sweepHours;
obj.mm = sweepMinutes
obj.dealId = theDealId;
$.ajax({
type: 'POST',
url: '@Url.Action("SubmitSweep", "Home")',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: obj,
success: function (data) {
alert("Success");
},
error: function (ob, errStr) {
alert("An error occured.Please try after sometime." + errStr + " " + ob.responseText);
}
});
}
I receive an error with the title tag reading Invalid JSON primitive: personID. It's worth noting that I pass the obj in the Data element.
If I change the data line to
data: JSON.stringify(obj),
I then get an error message with the title indicating that an item with the same key has already been added. In both of these scenarios, my controller breakpoint is never reached.
I'm using Visual Studio 2022, so my libraries are mostly up-to-date.
Any assistance or guidance would be greatly appreciated.
Cheers
J