I'm trying to send the request to controller but the console returns 500 error. What is the problem here?
Thanks in advance!
JS CODE:
$('.delete_btn').on('click', function () {
$.get("/List/Delete", { param: $(this).data('id') }, function (data) {
$('#modal_window').replaceWith('<div id="modal_window">' + data + '</div>');
$('#modal_window').show();
});
});
});
Controller:
//DELETE ITEM
public ActionResult Delete(int id)
{
H_Table item = db_connection.H_Table.Find(id);
db_connection.H_Table.Remove(item);
db_connection.SaveChanges();
return RedirectToAction("Index");
}
SCREENSHOT:
You are passing wrong paramert name.
Change your parameter name from param to id in ajax request.
$('.delete_btn').on('click', function () {
$.get("/List/Delete", { id: $(this).data('id') }, function (data) {
$('#modal_window').replaceWith('<div id="modal_window">' + data + '</div>');
$('#modal_window').show();
});
});
And your button must have data-id attribute. like this
<input type="button" class="delete_btn" value="Test" data-id="7" />
您正在执行删除操作,因此需要发出发布或删除请求,并且在控制器中需要通过[httpPost]装饰操作方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.