ASP.NET MVC form that redirects to a route
Hello is it possible to have开发者_Go百科 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?
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>
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();
}
精彩评论