1

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

它似乎在寻找我删除的删除视图。我只是保留 [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

如何直接调用删除帖子而不重定向?

4

3 回答 3

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 })
于 2013-04-17T20:08:17.370 回答
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" }
                   );
于 2013-04-17T20:25:23.177 回答
1

当您单击链接时,您只需使用retuestDelete导航到此 url 。GET您应该禁用默认链接操作,我认为最好使用 jquery ajax,而不需要额外的表单和 microsoft ajax 助手。

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

因为没有__RequestVerificationToken你会得到错误,即使使用POST请求。

于 2013-04-17T20:27:24.817 回答