2

I am attempting to use Html.BeginRouteForm to generate the action for a form in my ASP.NET MVC app. The route I am targeting is:

//CREATE
routes.MapRoute("map-config-post", "projects/{projectId}/mapconfigs",
    new { controller = controllerName, action = "Create" },
    new { httpMethod = new RestfulHttpMethodConstraint("POST") });

Unit testing and following the route in the app is successful. Now, I would like to create an HTML form with this route is it's URL:

@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{ 
    //form stuff
}

This results in an HTML form with a blank action attribute. Googling around, it seems like this is supposed to work. This is a "New" view, so the current (i.e., postback) route is

projects/1/mapconfigs/new

And I want it to post to

projects/1/mapconfigs

which is what the form action should be.

I can manually make this all happen if I don't use the helpers, but then I lose the nice auto-validation stuff on the client-side.

So, any ideas of what I am doing wrong? Hopefully it's clear what I am trying to do.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ruprict
  • 398
  • 4
  • 14

1 Answers1

2

It seems that the problem comes from your custom RestfulHttpMethodConstraint constraint. Using the default one works perfectly fine:

routes.MapRoute(
    "map-config-post",
    "projects/{projectId}/mapconfigs",
    new { controller = "Home", action = "Create" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

and then:

@using (Html.BeginRouteForm("map-config-post", new { projectId = Model.Project.Id }))
{ 
}

generates:

<form action="/projects/123/mapconfigs" method="post">

</form>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yup, but now it doesn't route to the Delete action if I post a form to that with the X-HTTP-Method-Override set to DELETE, which is what my custom constraint does. So, I had to add the 'httpMethod' attribute into my RouteValueDictionary in BeginRouteForm to make it work. Thanks for putting me on the right track! – Ruprict Jan 27 '11 at 16:46