1

I am trying to call [HttpPost] Delete from the view. on click I want load jQuery dialog and call post action method.

It seems it looking for Delete view which I deleted. I just kept the code for [HttpPost] to process.

index view

 <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>

Controller

 [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");
        }

Once I click on the Delete link page automatically redirecting to Delete view without jQuery dialog.

url is 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

How to call directly Delete post without redirect?

James123
  • 11,184
  • 66
  • 189
  • 343

3 Answers3

2

You are hitting the action with GET instead of POST.

Try

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

in place of

@Html.ActionLink("Delete", "Delete", new AjaxOptions { HttpMethod = "POST"}, 
          new { @class = "delete-link" , id =model.Id })
Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
Rohrbs
  • 1,855
  • 13
  • 11
  • 2
    @James123, use a form or ajax.actionlink post method – Dave Alperovich Apr 17 '13 at 20:11
  • 1
    Thanks, @DaveA. My only concern is that he now has a button instead of a regular hyperlink, so your suggestion of using `@Ajax.ActionLink()` is probably a better solution. – Rohrbs Apr 17 '13 at 20:17
1

You should use RoutLink for this

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

In your RouteConfig class

   routes.MapRoute(
                 name: "DeleteMember",
                 url: "Delete/{id}",
                 defaults: new { controller = "Member", action = "DeleteConfirmed" }
                   );
HaBo
  • 13,999
  • 36
  • 114
  • 206
1

When you click Delete link you just navigating to this url with GET retuest. You should disable default link action and I think it's better to use jquery ajax, without extra forms and microsoft ajax helpers.

This question is what you need: jQuery Ajax calls and the Html.AntiForgeryToken()

Because without __RequestVerificationToken you will get error, even using POST request.

Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47