代码之家  ›  专栏  ›  技术社区  ›  Paul

ASP.NET MVC从获取表单发布出站路由

  •  0
  • Paul  · 技术社区  · 15 年前

    我有一条像这样的路线:

    new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional, sku = UrlParameter.Optional}
    

    我有一张这样的表格:

      <%using(Html.BeginForm("PhotoGallery", "ControllerName", FormMethod.Get)) {%>
      <%:Html.Hidden("filtertype", "1")%>
      <%:Html.DropDownList("filtervalue", ViewData["Designers"] as SelectList, "Photos by designer", new { onchange = "selectJump(this)" })%>
      <%}%>
    

    现在提交表单时,我会得到作为查询字符串附加到URL的表单值(?filtertype=1等)是否有方法使此表单使用路由来呈现URL?

    所以表单会发布一个如下所示的URL:

    www.site.com/photo-gallery/1/selectedvalue
    

    不喜欢”

    www.site.com/photo-gallery?filtertype=1&filtervalue=selectedvalue
    

    谢谢!

    1 回复  |  直到 15 年前
        1
  •  1
  •   Clicktricity    15 年前

    如果您只有一些参数,MVC将创建一个查询字符串类型的URL,除非它找到一个完全匹配的URL。

    建议您需要类似以下内容:

    routes.Add("testRoute", 
    new Route("/{action}/{controller}/{slug}/{filtertype}/{filtervalue}",
    new { controller = "ControllerName", action = "PhotoGallery", slug = "photo-gallery", filtertype = UrlParameter.Optional, filtervalue = UrlParameter.Optional}
    );
    

    确保这在您现有路线之前