ASP.NET MVC 路由, Html.BeginForm
<% using (Html.BeginForm("SearchByZip", "Dealer", new { zip = ""}, FormMethod.Get))
{ %>
<div>
<input type="text" class="padLeft" name="Zip" id="Zip" style="width: 200px" />
<input type="submit" class="btnFind" value="Find" />
</div>
<% } %>
这给了我 url“Dealer/SearchByZip?Zip=12345” 我想以这样的结尾:“Dealer/Zip/12345” (如果我手动输入网址“Dealer/Zip/12345”,它会返回正确的结果,但是当我单击“提交”时,它会显示“Dealer/SearchByZip?Zip=12345” 我缺少什么?
routes.MapRoute(
"DealerSearchByZip",
"Search/Zip/{zip}",
new { Controller = "Dealer", action = "SearchByZip", zip = "" }
);
<% using (Html.BeginForm("SearchByZip", "Dealer", new { zip = ""}, FormMethod.Get))
{ %>
<div>
<input type="text" class="padLeft" name="Zip" id="Zip" style="width: 200px" />
<input type="submit" class="btnFind" value="Find" />
</div>
<% } %>
This gives me the url "Dealer/SearchByZip?Zip=12345"
I would like to end up with this: "Dealer/Zip/12345"
(if I manually type in the url "Dealer/Zip/12345" it returns the right results, but when I click in submit it comes up with "Dealer/SearchByZip?Zip=12345"
What am I missing?
routes.MapRoute(
"DealerSearchByZip",
"Search/Zip/{zip}",
new { Controller = "Dealer", action = "SearchByZip", zip = "" }
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为“Zip”是表单中的输入字段,而不是路由数据。因此,当呈现页面时,它会使用默认路由创建一个 url(“DealerSearchByZip”路由不匹配,因为未将 Zip 作为路由数据给出)。
您可以通过 javascript 完成此操作,方法是在更新“zip”字段时更新表单上的“action”属性。
使用 jQuery 的示例:
或者,由于 Zip 是您唯一担心的值,
This is happening because "Zip" is an input field in your form, not route data. So, when the page is rendered it creates a url using the default route ("DealerSearchByZip" route wasn't matched because Zip wasn't given as route data).
You could accomplish this via javascript, by updating the "action" attribute on the form when the "zip" field is updated.
Example using jQuery:
Or, since Zip is the only value you're worried about,