1 回答

TA貢獻1860條經驗 獲得超9個贊
您應該知道的第一件事是RedirectToAction在 AJAX 調用中不起作用,您應該將 URL 傳遞給重定向,location.href如下所示:
控制器動作
[HttpPost]
public ActionResult Delete(int id)
{
db.Delete<Logs>(id);
// other stuff
string url = this.Url.Action("Index", "Log", new { id = id });
return Json(url);
}
jQuery
$.post("@Url.Action("Delete", "Log")", { id: selectedid }, function (result) {
window.location.href = result;
});
或者更好地創建一個包含要通過 AJAX 更新的所有元素的局部視圖,然后將其傳遞給success部分:
控制器動作
[HttpPost]
public ActionResult Delete(int id)
{
db.Delete<Logs>(id);
// other stuff
return PartialView("PartialViewName");
}
jQuery
$.post("@Url.Action("Delete", "Log")", { id: selectedid }, function (result) {
$('#targetElement').html(result);
});
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報