您是否可以拥有一个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: //机器/ SAT / XXX

有可能吗?

有帮助吗?

解决方案

您真的关心您导航到的网址,或者您只关心用户看到的下一个网址是什么?

如果您只关心用户看到的网址,则无需使用您尝试的方法。

你可以做的是发布一个读取“nomsat”的帖子。参数,然后重定向到另一个具有您想要的URL的操作。

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

    public ActionResult Detail(string nomsat)
    {
       ...
       return View();
    }

其他提示

不,您无法使用HTML表单添加路由参数。

您可以使用Javascript函数模拟行为。像这样:

<fieldset>
    <legend>Opciones</legend>
    <%= Html.DropDownList("nomSat")%>
    <input type="button"  
     onclick="window.location=('/<%=Url.Action("CanviaOpcions", "Sat") %>/' + 
     $('#nomSat').val())" />
</fieldset>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top