Reputation: 1934
Using knockout I am trying to send data, from my UI to the controller. This is the javascript used to send my ajax request(PUT)
var model = new Object();
model.StudentID = "";
model.ActiveProgram = "";
model.ProgramDesc = self.programData();
model.Cohorts = self.associationData();
model.LoadIntent = self.loadIntentData();
model.Francophone = self.frenchData();
model.Gender = self.genderData();
$.ajax({
url: putStudentRegRequirementsUrl,
type: "PUT",
contentType: jsonContentType,
dataType: "json",
data: JSON.stringify(model),
//jsonData:model,
success: function (data) {
$('#notificationHost').notificationCenter('addNotification', { message: "Updated.", type: "info" });
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status != 0)
{
$('#notificationHost').notificationCenter('addNotification', { message: "Unable to update registration requirement.", type: "error"});
}
}
});
But when I debug it to see my controller, the string comming in is blank. This is my controller
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(string regRequirementJson)
{
try
{
var regRequirementModel = JsonConvert.DeserializeObject<RegistrationRequirement>(regRequirementJson);
var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, regRequirementModel);
return Json(response);
}
catch( Exception ex)
{
Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Error updating Registration Requirement.");
}
}
Upvotes: 1
Views: 82
Reputation: 9642
Action will parse parameters from client by its name, so you need to pass parameter with name regRequirementJson
contains your json. So change this line
data: JSON.stringify(model)
to
data: { regRequirementJson: JSON.stringify(model) }
and remove contentType: jsonContentType
.
Or you can try another way. Since ASP.NET can deserialize json by itself you can keep your js code as is and update your controller to
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement regRequirementModel )
{
try
{
var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, regRequirementModel);
return Json(response);
}
catch( Exception ex)
{
Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Error updating Registration Requirement.");
}
Upvotes: 3
Reputation: 74
Since you are sending a "RegistrationRequirement" object then in your controller you can do it this way :
[HttpPut]
public async Task<JsonResult> UpdateRegistrationRequirementAsync(RegistrationRequirement registrationRequirement)
{
try
{
var response = await ServiceClient.L09PutRegistrationRequirementAsync(CurrentUser.PersonId, registrationRequirement);
return Json(response);
}
catch( Exception ex)
{
Logger.Debug(ex, "Error updating Registration Requirement for user failed.");
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Error updating Registration Requirement.");
}
}
Upvotes: 1