gpt4 book ai didi

asp.net - ASP.NET MVC 中的搜索路由

转载 作者:行者123 更新时间:2023-12-03 03:18:03 25 4
gpt4 key购买 nike

我的主页中有一个简单的搜索表单以及一个搜索 Controller 和 View 。我正在尝试获取字符串搜索词“myterm”的以下路径(例如):根/搜索/myterm

母版页中的表单:

<% using (Html.BeginForm("SearchResults", "Search", FormMethod.Post, new { id = "search_form" }))
{ %>
<input name="searchTerm" type="text" class="textfield" />
<input name="search" type="submit" value="search" class="button" />
<%} %>

Controller 操作:

public ActionResult SearchResults(string searchTerm){...}

我正在使用的路线:

routes.MapRoute(
"Search",
"search/{term}",
new { controller = "Search", action = "SearchResults", term = (string)null }
);

routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);

无论我输入什么搜索词,我总是得到不带搜索词的网址“root/search”。

谢谢。

最佳答案

您在 beginform 标记中使用 id,在 route 使用 {term}。

两者需要匹配。

关于asp.net - ASP.NET MVC 中的搜索路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com