从mvc4中的视图调用删除帖子?

时间:2013-04-17 20:05:42

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

我正试图从视图中调用[HttpPost] Delete。点击我想加载jQuery对话框并调用post action方法。

它似乎正在寻找我删除的删除视图。我只是保留了[HttpPost]的代码来处理。

索引视图

 <ul class="dropdown-menu">
  @{
     @Html.TryPartial("_actions", model)
        <li> @Html.ActionLink("Edit", "Edit", new {id =model.Id})</li>
        <li class="divider"></li>
        <li>@Html.ActionLink("Delete", "Delete", new {id =model.Id},new { @class = "delete-link" })</li>
  }
  </ul>

控制器

 [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Byte[] id)
        {

            var committeeMember = db.Committee_Member.FirstOrDefault(x => x.Committee_Member_Id == id);

            if (committeeMember != null)
            {
                committeeMember.Cancelled = 1;
                db.Entry(committeeMember).State = EntityState.Modified;
                db.SaveChanges();
                Success("Your activity was deleted!");
                return RedirectToAction("Index", new { id = committeeMember.Customer_Number });
            }

            Error("there were some errors in your form.");
            return RedirectToAction("Index");
        }

点击Delete链接页面后,自动重定向到Delete视图,而不使用jQuery对话框。

网址是 http://company.com:55253/Member/Delete/AAAAAAICyns%3d

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Member/Delete/AAAAAAICyns=

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

如何直接拨打删除帖子而不重定向?

3 个答案:

答案 0 :(得分:2)

您使用GET代替POST进行操作。

尝试

@using (Html.BeginForm()) {
    <input type="submit" value="Delete" />
    @Html.HiddenFor(m => m.Id)
}

取代

@Html.ActionLink("Delete", "Delete", new AjaxOptions { HttpMethod = "POST"}, 
          new { @class = "delete-link" , id =model.Id })

答案 1 :(得分:1)

你应该使用RoutLink

@Html.RouteLink(linkname: "Delete", routeName:"DeleteMember", objectRouteValues: new {id =model.Id},objectHtmlValues: new { @class = "delete-link" })

在RouteConfig类中

   routes.MapRoute(
                 name: "DeleteMember",
                 url: "Delete/{id}",
                 defaults: new { controller = "Member", action = "DeleteConfirmed" }
                   );

答案 2 :(得分:1)

点击Delete链接后,您只需GET重新导航即可导航到此网址。你应该禁用默认链接操作,我认为最好使用jquery ajax,不需要额外的表单和microsoft ajax助手。

这个问题就是您所需要的:jQuery Ajax calls and the Html.AntiForgeryToken()

因为没有__RequestVerificationToken,即使使用POST请求,您也会收到错误。