I am trying to update records in a database using AJAX requests. I'm able to insert using similar to the below, but am not successful when I attempt to update or delete.
Here is my ajax PUT method:
$('#updateCr').click(function(e) {
e.preventDefault();
var updatedData = {
CrId: $('#CrId').val(),
CrName: $('#CrName').val(),
CrStatus: $('#CrStatus').val(),
CrPriority: $('#CrPriority').val(),
CrTitle: $('#CrTitle').val(),
CrDescription: $('#CrDescription').val()
};
console.log(changeRequest);
$.ajax({
type: "PUT",
url: "@Url.Action("MyControllerAction", "Home")",
content: "application/json; charset=utf-8",
dataType: "json",
data: updatedData,
cache: false,
success: function(d) {
if (d.success == true)
window.location = "index.html";
else {}
},
error: function(xhr, textStatus, errorThrown) {
// TODO: Show error
}
});
And here is my Controller
[HttpPut]
public JsonResult MyControllerAction(UpdatedDataObject updatedData)
{
try
{
myModel.UpdateDataMethod(updatedData);
}
catch (Exception e)
{
var theerrror = e.ToString();
return Json(new { result = theerrror });
}
return Json(new { result = "Success" });
}
When submitting the data, the controller is triggered, but it is not receiving a JSON object. What am I doing wrong here?