PositiveGuy
PositiveGuy

Reputation: 47783

Delete Action not working MVC 3

Route I have defined is:

    map.Route(new Route("Cars/{id}/Delete",  
              new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
              new MvcRouteHandler()));

In my view I've got:

<a href="/Car/@Model.Id/Delete">Delete</a>

Which when run tries to send a request to http://oursite/Car/122/Delete

My delete action in this Car controller looks like this:

public ActionResult Delete(int id)
{
    //code is here
}

I noticed a couple things:

  1. If I run this same code locally via my PC, the delete works flawlessly and is able to get to my action method. I'm running this over IIS 7 / Win 7

  2. On our dev server, it's setup obviously via IIS7 but this route fails and says it can't find the route on our route table. But this is the SAME route table class I am using locally...so why would I get this:

No route in the route table matches the supplied values.

But why would that not work on a dev server? I see the setup identical in IIS for the most part as far as I can see when I compare my local setup to the server's.

I noticed that also whether localhost or server, if I try and put an [HttpDelete] attribute on my delete action, it doesn't find my action method and I get an error saying it can't find that method. So not sure why when I take that off, the delete works (localhost only)

Upvotes: 1

Views: 944

Answers (5)

Chris Gessler
Chris Gessler

Reputation: 23123

There's likely a difference in the URL paths between localhost and oursite. The path "/Car/@Model.Id/Delete" is hard-coded, not resolved and may not work in all environments. As suggested in other answers, use an MVC helper like @Html.ActionLink or @Url.RouteUrl to resolve the path for the local environment.

Upvotes: 0

MikeSW
MikeSW

Reputation: 16378

First name that route

map.Route("DeleteCar",new Route("Cars/{id}/Delete",  
          new RouteValueDictionary(new { controller = "Car", action = "Delete"}),
          new MvcRouteHandler()));

Then

<a href="@Url.RouteUrl("DeleteCar",new{id=Model.Id})">Delete</a>

Unless that link goes to a warning screen, I strongly suggest that a delete should be a POST or even a DELETE(I think it can be set via ajax)

Upvotes: 0

JasCav
JasCav

Reputation: 34652

I think you answered your own question. There is no route in the route table that matches your supplied values. You could write that route to do that by writing this in your Global.asax.cs file:

public class Global : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    // Specify routes
    RouteTable.Routes.Add(new Route
    {
      Url = "[controller]/[id]/[action]",
      Default = new { controller = "Car" },
      RouterHandler = typeof(MvcRouteHandler)
    });
  }
}

Or, you can use existing routes (my personal recommendation) to use the Delete function in your Car controller. To do that, try switching your code to this:

<a href="/Car/Delete/@Model.Id">Delete</a>

Upvotes: 0

Varun Naik
Varun Naik

Reputation: 160

Could you please share code for the View. How do you build the 'a' tag in the view?

Regarding the [HttpDelete] attribute, it means that the method needs the HTTP 'DELETE' request. The 'a' tag always has a GET request. Please refer this link

Upvotes: 0

user596075
user596075

Reputation:

Use a helper to generate your link:

@Html.ActionLink("Delete", "Delete", "Car");

The first parameter is your link text, the second is your Action method name, and the third is your Controller name.

See this MSDN Reference on ActionLink().

Upvotes: 1

Related Questions