I am attempting to send a JSON object and two JavaScript arrays in the same AJAX call.
However, I keep encountering the following error:
System.ArgumentException: Invalid JSON primitive: object
I suspect that this error is related to the different variable types being passed.
Can you spot any obvious errors?
Thank you
var requestData = {
"deptCode": userVar,
"roundID": parseInt(roundIDVar),
"moduleCode": moduleCodeVar,
"priority": parseInt(priorityVar),
"day": parseInt(dayVar),
"start": parseInt(timeVar) - 8,
"length": parseInt(lengthVar),
"weeks": weeksNum,
"capacity": parseInt(studentsVar),
"type": roomTypeVar,
"otherReqs": otherReqs
};
var obj = JSON.stringify(requestData);
$.ajax({
type: 'POST',
url: '/create/Submit',
error: function (xhr, ajaxOptions, thrownError) {
alert("Submission Failed. Please Reload and Try Again.");
},
data: { JSONdata: obj, weeks: weeksVar, facilities: facilitiesValue },
datatype: 'html',
contentType: 'application/json',
processData: false,
async: false,
success: function (data) {
alert(data);
}
});
Controller
public ActionResult Submit(request JSONdata, String[] Weeks, String[] facilities) {
ViewBag.module = JSONdata.weeks;
if(JSONdata.otherReqs == null){
JSONdata.otherReqs = "None";
}
JSONdata.sent = 1;
JSONdata.status = 0;
JSONdata.viewed = 0;
JSONdata.booked = 0;
db.requests.Add(JSONdata);
try
{
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
if(Convert.ToInt16(JSONdata.weeks) == 1){
for (var i = 0; i < Weeks.Length; i++) {
Weeks_request newWeek = new Weeks_request();
newWeek.week = Convert.ToInt16(Weeks[i]);
newWeek.requestID = JSONdata.requestID;
db.weeks_request.Add(newWeek);
db.SaveChanges();
}
}
return View();
}