2013-09-29 55 views
2

我想向我的用户展示一个表单,他们可以输入开始邮政编码,点击一个按钮,并最终在提供已知邮政编码的驾驶路线的页面上。MVC @ Html.BeginForm匹配不正确的路线

我想匹配的路径定义为:

routes.MapRoute("Directions", "Directions/{Name}/{StartPostCode}-to-{DestinationPostCode}", new { controller = "Directions", action = "Index" }); 

我使用以下代码来呈现的形式:

@using (@Html.BeginForm("Index", "Directions", new { DestinationPostCode = Model.postcode, Name = Model.nameURLized }, FormMethod.Get)) 
{ 
    <input type="text" name="StartPostCode" id="StartPostCode" class="inputGreyed" value="Enter Postcode" onclick="ToggleDefaultText(this)"/> 
    <input type="submit" value="Get Directions" /> 
} 

的问题是,我在/Directions/Index?StartPostCode=abc123结束。它缺少目标邮政编码和名称键值对。这当然意味着我最终会以错误的路线处理我的请求。我已经用Phil Haacks路由调试器证明了这一点。

我已经测试过直接进入/Directions/TheName/ABC123-to-DT83PX,它按预期工作。事实上,我尝试使用下面的代码来建立一个链接:

@Html.ActionLink("Directions Generated", "Index", "Directions", new { StartPostCode = "ABC123", DestinationPostCode = Model.postcode, Name = Model.nameURLized }, new { @class = "button", @title = "More details on " + Model.name }) 

任何帮助将不胜感激。

回答

1

请执行下列操作:

一)添加默认路由:

routes.MapRoute(null, "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } 

B)转至隐藏的输入字段DestinationPostCode和LISTINGNAME值。

c)用途:

@using (@Html.BeginForm("Index", "Directions")) { your code }