views:

204

answers:

2

Hello is it possible to have an ASP.NET MVC form that uses the routes defined in Global.asax to post its values (via a GET request)? I have the form defined like this:

<% using (Html.BeginForm("CanviaOpcions","Sat",FormMethod.Get))
    { %>
    <fieldset>
        <legend>Opciones</legend>
        <%= Html.DropDownList("nomSat")%>
        <input type="submit" />
    </fieldset>
<% } %>

and the following route in my global.asax:

routes.MapRoute(
    "Canvia Opcions",
    "Sat/{nomSat}",
    new { controller = "Sat", action = "CanviaOpcions" }
    );

I want that after a submit the form with nomSat having the value XXX to have the following URL in my browser: http://machinename/sat/XXX

Is it possible?

+2  A: 

No, you can't add to the routing parameters using an HTML form.

You can simulate the behaviour with a Javascript function though. Like this :

<fieldset>
    <legend>Opciones</legend>
    <%= Html.DropDownList("nomSat")%>
    <input type="button"  
     onclick="window.location=('/<%=Url.Action("CanviaOpcions", "Sat") %>/' + 
     $('#nomSat').val())" />
</fieldset>
çağdaş
Thanks. I think I'll leave it as it is now, because I think that with javascript it could lead to inconsistent behavior...
Carles
You're welcome. And yes, I wouldn't use JS either in this case, unless it'a MUST to have the URLs that way.
çağdaş
+2  A: 

Do you really care about the URL you navigate to, or do you only care about what the next URL the user sees is?

If you just care about the URL the user sees, then you don't need to use the method you are trying.

What you could do is have a post action that reads in the "nomsat" parameter, then redirect to another action that has the URL you are wanting.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string nomsat)
    {
        ...
        return RedirectToAction("Detail", new RouteValueDictionary {{"nomsat", nomsat}});
    }

    public ActionResult Detail(string nomsat)
    {
       ...
       return View();
    }
Carlton Jenke
Nice. I does need an extra redirect though. It would be nicer to have an special Html.BeginForm which used ASP.NET MVC routing...
Carles