发出带有确认消息的HTTP发布请求以进行删除

时间:2014-10-22 13:06:05

标签: asp.net-mvc asp.net-mvc-4

我想通过获取用户&的确认来删除项目我想用POST方法删除它。

这是我的代码段

  @Html.ActionLink("Delete","Delete",new { id=item.UserId },new AjaxOptions {HttpMethod = "POST", Confirm = "Do you really want to delete this?"})

&安培;我的行动方法是:

    [HttpPost]
    public ActionResult Delete(int? id, LearningMVC.Models.User userDetails)
    {
        try
        {
            var dbContext = new MyDBDataContext();
            var user = dbContext.Users.FirstOrDefault(userId => userId.UserId == id);
            if (user != null)
            {
                dbContext.Users.DeleteOnSubmit(user);
                dbContext.SubmitChanges();
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    } 

如果删除[HttpPost]属性,它只删除记录而不进行任何确认。它使用[HttpPost]属性给出404错误。 我在布局页面的所述顺序中包含了jquery-1.5.1.min.js,unobtrusive-ajax.js,MicrosoftAjax.js,MicrosoftMvcAjax.js。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我相信你想写:

@Ajax.ActionLink("Delete", "Delete",
    new { id = item.UserId },
    new AjaxOptions { HttpMethod = "POST", Confirm = "Do you really want to delete this?" })

只需使用AjaxHelper代替HtmlHelper