Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the routing engine for form submissions in ASP.NET MVC Preview 4

I'm using ASP.NET MVC Preview 4 and would like to know how to use the routing engine for form submissions.

For example, I have a route like this:

routes.MapRoute(
            "TestController-TestAction",
            "TestController.mvc/TestAction/{paramName}",
            new { controller = "TestController", action = "TestAction", id = "TestTopic" }
            );

And a form declaration that looks like this:

<% using (Html.Form("TestController", "TestAction", FormMethod.Get))
   { %>
     <input type="text" name="paramName" />
     <input type="submit" />
<% } %>

which renders to:

<form method="get" action="/TestController.mvc/TestAction">
  <input type="text" name="paramName" />
  <input type="submit" />
</form>

The resulting URL of a form submission is:

localhost/TestController.mvc/TestAction?paramName=value

Is there any way to have this form submission route to the desired URL of:

localhost/TestController.mvc/TestAction/value

The only solutions I can think of are to create a separate action that just checks the request parameters, or to use Javascript.

like image 322
Matt Mitchell Avatar asked Aug 12 '08 05:08

Matt Mitchell


1 Answers

Solution:

public ActionResult TestAction(string paramName)
{
    if (!String.IsNullOrEmpty(Request["paramName"]))
    {
        return RedirectToAction("TestAction", new { paramName = Request["paramName"]});
    }
    /* ... */
}
like image 56
Matt Mitchell Avatar answered Oct 20 '22 02:10

Matt Mitchell