What is the best way to delete an item in MVC? I have a list of items. Each row will contain a "Delete" link. I want the Delete link to prompt for confirmation, then delete the item from the datastore and refresh the page with the new data.
在MVC中删除项目的最佳方法是什么?我有一个项目清单。每行都包含一个“删除”链接。我希望删除链接提示确认,然后从数据存储区中删除该项目并使用新数据刷新页面。
Here is my view code:
这是我的观看代码:
<%: Ajax.ActionLink(
"Delete"
,"Delete"
, new { id=item.FooId}
, new AjaxOptions()
{
Confirm="Are you sure that you want to delete this item?"
, HttpMethod = "post"} ) %>
And here is my controller code:
这是我的控制器代码:
[HttpPost]
public ActionResult Delete(int id)
{
try
{
var success = FooService.Deletefoo(id);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
The record is being deleted, however the page is not refreshing. The only thing that I can think of is that RedirectToAction only works for different pages, not the same page.
该记录正在删除,但页面不刷新。我唯一能想到的是RedirectToAction只适用于不同的页面,而不适用于同一页面。
How do I get the page to refresh?
如何刷新页面?
2 个解决方案
#1
6
You're page is not refreshing because the AJAX call is not going to honor a 302 - the RedirectToAction() is used when the entire browser is refreshing. If you're going to use AJAX for your delete link then have a look at this post for all example code. On complete, it does a javascript window.location.reload(); in order to refresh the page. This follows the PRG pattern.
您的页面没有刷新,因为AJAX调用不会遵循302 - 当整个浏览器刷新时使用RedirectToAction()。如果您要将AJAX用于删除链接,那么请查看此帖子以获取所有示例代码。完成后,它会执行一个javascript window.location.reload();为了刷新页面。这遵循PRG模式。
Another approach is the use to not use AJAX. Here is an example for that.
另一种方法是不使用AJAX。这是一个例子。
Overall, you're fine sticking with your AJAX approach.
总的来说,你很好地坚持使用你的AJAX方法。
#2
0
The another way is to remove that control/html element through javascript. You can call this script when your Ajax request gets completed.
另一种方法是通过javascript删除该控件/ html元素。您可以在Ajax请求完成时调用此脚本。
<%: Ajax.ActionLink(
"Delete"
,"Delete"
, new { id=item.FooId}
, new AjaxOptions()
{
OnSuccess="deleteElement"
,Confirm="Are you sure that you want to delete this item?"
, HttpMethod = "post"} ) %>
OnSuccess
Option tells the Ajax helper to call the method when ajax requests gets completed successfully.
OnSuccess Option告诉Ajax帮助程序在ajax请求成功完成时调用该方法。
#1
6
You're page is not refreshing because the AJAX call is not going to honor a 302 - the RedirectToAction() is used when the entire browser is refreshing. If you're going to use AJAX for your delete link then have a look at this post for all example code. On complete, it does a javascript window.location.reload(); in order to refresh the page. This follows the PRG pattern.
您的页面没有刷新,因为AJAX调用不会遵循302 - 当整个浏览器刷新时使用RedirectToAction()。如果您要将AJAX用于删除链接,那么请查看此帖子以获取所有示例代码。完成后,它会执行一个javascript window.location.reload();为了刷新页面。这遵循PRG模式。
Another approach is the use to not use AJAX. Here is an example for that.
另一种方法是不使用AJAX。这是一个例子。
Overall, you're fine sticking with your AJAX approach.
总的来说,你很好地坚持使用你的AJAX方法。
#2
0
The another way is to remove that control/html element through javascript. You can call this script when your Ajax request gets completed.
另一种方法是通过javascript删除该控件/ html元素。您可以在Ajax请求完成时调用此脚本。
<%: Ajax.ActionLink(
"Delete"
,"Delete"
, new { id=item.FooId}
, new AjaxOptions()
{
OnSuccess="deleteElement"
,Confirm="Are you sure that you want to delete this item?"
, HttpMethod = "post"} ) %>
OnSuccess
Option tells the Ajax helper to call the method when ajax requests gets completed successfully.
OnSuccess Option告诉Ajax帮助程序在ajax请求成功完成时调用该方法。