Reputation: 4496
I'm trying to call this method:
// POST: /Manufacturer/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Manufacturer manufacturer = unitOfWork.manufacturerRepository.GetByID(id);
unitOfWork.manufacturerRepository.Delete(manufacturer);
unitOfWork.Save();
return Json(new { ok = true, newurl = Url.Action("Index", "Manufacturer") });
}
with this ajax:
$.ajax({
url: "/Manufacturer/Delete/" + $('#Id').val(),
type: "POST",
contentType: "application/json; charset=utf-8",
headers: {
'RequestVerificationToken': '@TokenHeaderValue()'
},
data: { id: $('#Id').val() },
error: function (data) {
alert("error" + data);
},
success: function (data) {
if (data.ok)
{
$("#Modal").modal('hide');
}
else {
$('.modal-body').html(data);
}
}
})
I checked in fiddler And I can see the ID is passed correctly. I Put break point at the start of a method but it isn't hit. Call crashes before. What should I do to find something what is causing this crashes?
data from fiddler:
POST http://localhost:1809/Manufacturer/Delete/34 HTTP/1.1
Host: localhost:1809
Connection: keep-alive
Content-Length: 5
Accept: */*
RequestVerificationToken: TQY9NrchHlFT6IBaTv1R4daiwGoRH0yv3gHJwpatGj
Origin: http://localhost:1809
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:1809/Admin/Index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: __RequestVerificationToken=r5YwV-v
id=34
Upvotes: 1
Views: 1644
Reputation: 139
Try to remove the id from the post URL. This way it passes it to the right method and id can be bound to the posted data "id"
$.ajax({
url: "/Manufacturer/Delete/"
.......
When passing the id it should be in a JSON string, here we can use JSON.stringify(value[, replacer [, space]]).
data: JSON.stringify({ id: $('#Id').val() })
You can also use XML if u want and parse it to a string by:
(new XMLSerializer()).serializeToString(xml);
Upvotes: 1
Reputation: 2271
Remove the id from the POST
Url.Id in Url is used in a GET
request.But if you want to use POST Use Json.stringify()
in the data part of ajax call.
$.ajax({
url: "/Manufacturer/Delete",
type: "POST",
contentType: "application/json; charset=utf-8",
headers: {
'RequestVerificationToken': '@TokenHeaderValue()'
},
data: JSON.stringify({ id: $('#Id').val() }),
error: function (data) {
alert("error" + data);
},
success: function (data) {
if (data.ok)
{
$("#Modal").modal('hide');
}
else {
$('.modal-body').html(data);
}
}
})
Upvotes: 3