提交表单不会在mvc

时间:2016-02-02 12:28:50

标签: c# asp.net-mvc forms url-routing

我有一个搜索表单,我希望在提交表单时生成一个干净的URL。

我为此创建了一个自定义路线:

routes.MapRoute(
                name: "Custom",
                url: "{controller}/{action}/{KEY}/{BN}/{SBN}/{CN}",
                defaults: new { controller = "Home", action = "Index" }
            ); 

这是我的表格:

@using (Html.BeginForm("TestUrl", "Home", FormMethod.Get))
{
    <input type="text" name="KEY" value="q"/>
    <input type="text" name="BN" value="Miscellaneous" />
    <input type="text" name="SBN" value="Specialized Photographic Equipment" />
    <input type="text" name="CN" value="South Georgia and the South Sandwich Islands" />
<input type="submit" value="Search"/>
}

我的行动如下:

 public ActionResult TestUrl(string KEY,string BN,string SBN, string CN)
        {
            return View();
        }

当我提交表单时,会生成一个这样的网址:

  

http://localhost:59548/Home/TestUrl?KEY=q&BN=Miscellaneous&SBN=Specialized+Photographic+Equipment&CN=South+Georgia+and+the+South+Sandwich+Islands

我的ActionLink正在生成一个干净的网址:

@Html.ActionLink("Test Link", "TestUrl", "Home", new { KEY = "0", BN = "Miscellaneous", SBN = "Specialized Photographic Equipment", CN = "South Georgia and the South Sandwich Islands" }, new { })

ActionLink会生成这个干净的网址:

  

http://localhost:59548/Home/TestUrl/0/Miscellaneous/Specialized%20Photographic%20Equipment/South%20Georgia%20and%20the%20South%20Sandwich%20Islands

我提交表单时如何删除查询字符串?

1 个答案:

答案 0 :(得分:1)

您缺少的是在设置中包含您的路线变量。将它们作为路线的一部分,而且也在匿名物体内,这不仅仅是足够的。

routes.MapRoute(
            name: "Custom",
            url: "{controller}/{action}/{KEY}/{BN}/{SBN}/{CN}",
            defaults: new { controller = "Home", action = "Index",
                KEY = "", BN = "", SBN = "", CN = "" }
        ); 
相关问题