Teije
Teije

Reputation: 655

HttpPost won't be called

I have been at this for an hour now and I haven't got a clue what I am doing wrong.

I got the following ActionLink:

@Html.ActionLink("Remove", "RemoveActivity", "Dashboard", new { id = a.Id },htmlAttributes: null)

This targets the following Method in my DashboardController:

[HttpPost]
public ActionResult RemoveActivity(int id)
{
    activityRepo.Delete(activityRepo.GetById(id));

    return RedirectToAction("ActivityDetails");
}

For some reason this error gets returned:

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: /Dashboard/RemoveActivity/564

A table row with the Id of 564 does exist in the database. It worked a few hours ago. Any help is appreciated. I am clueless!

EDIT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace HaarlemFestival_Web
{
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
}

Upvotes: 1

Views: 195

Answers (3)

user9405863
user9405863

Reputation: 1514

You can try this , very simple way

In you view just place anchor tag: // make sure you have Id coming here

 <a href="#" onclick="RemoveActivity(@a.Id)">Remove</a> 

Jquery block:

 <script type="text/javascript">

        function RemoveActivity(index) {

                    var urlBase = '/Dashboard/RemoveActivity/';
                    $.ajax({
                        url: urlBase,
                        data: { id: index },
                        success: function (result) {                             
                        },
                        error: function (ex) {
                        }
                    });
                }                

</script>

keep your controller action as post method only. I hope this helps.

Upvotes: 0

user3559349
user3559349

Reputation:

@Html.ActionLink() generates a <a> tag which makes a GET, not a POST. You need to include a <form> element and submit the value to your POST method

@using (Html.BeginForm("RemoveActivity", "Dashboard", new { id = a.Id }))
{
    <input type="submit value="Remove" />
}

and you can style your submit button to look like a link if that is what you want visually

I also suggest you add the @Html.AntiForgeryToken() method in the <form> and add the [ValidateAntiForgeryToken] attribute to your method to prevent CSRF attacks.

You should also consider validating that the current user does have the permission to delete that record.

Note that since you method is changing data, it should be a POST, so do not be tempted to just remove the [HttpPost] attribte from your method so that the link works.

Upvotes: 2

Alex Nguyen
Alex Nguyen

Reputation: 1080

Because @Html.ActionLink will render an anchor tag, Clicking on which is always "GET" request. So if you want an HTTP-Post method you need to override its behavior using javascript like this:

 @Html.ActionLink("Remove", "RemoveActivity", "Dashboard", new { id = a.Id ,@class="post_link"},htmlAttributes: null);

[HttpPost]
public String RemoveActivity(int id)
{
    activityRepo.Delete(activityRepo.GetById(id));

    return "Remove/ActivityDetails";
}

    <script type="text/javascript">
  $(function(){
    $("a.post_link").click(function(e){
      e.preventDefault();
      $.post($(this).attr("href"),function(data){
          //got your redirection link and do a redirection request at here
          window.location = data;
      });
    });    
  });    
</script>

Upvotes: 1

Related Questions