您是否可以拥有一个ASP.NET MVC表单,该表单使用Global.asax中定义的路由发布其值(通过GET请求)?我的表格定义如下:
<% using (Html.BeginForm("CanviaOpcions","Sat",FormMethod.Get))
{ %>
<fieldset>
<legend>Opciones</legend>
<%= Html.DropDownList("nomSat")%>
<input type="submit" />
</fieldset>
<% } %>
以及我的global.asax中的以下路由:
routes.MapRoute(
"Canvia Opcions",
"Sat/{nomSat}",
new { controller = "Sat", action = "CanviaOpcions" }
);
我希望在提交带有值XXX的nomSat的表单后,在我的浏览器中显示以下网址:http://machinename/sat/XXX
有可能吗?
答案 0 :(得分:2)
不,您无法使用HTML表单添加路由参数。
您可以使用Javascript函数模拟行为。像这样:
<fieldset>
<legend>Opciones</legend>
<%= Html.DropDownList("nomSat")%>
<input type="button"
onclick="window.location=('/<%=Url.Action("CanviaOpcions", "Sat") %>/' +
$('#nomSat').val())" />
</fieldset>
答案 1 :(得分:2)
您真的关心您导航到的网址,或者您只关心用户看到的下一个网址是什么?
如果您只关心用户看到的网址,则无需使用您尝试的方法。
您可以执行的操作是读取“nomsat”参数,然后重定向到另一个包含您想要的URL的操作。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string nomsat)
{
...
return RedirectToAction("Detail", new RouteValueDictionary {{"nomsat", nomsat}});
}
public ActionResult Detail(string nomsat)
{
...
return View();
}