使用 MVC 4
我有一个正在创建搜索框的部分 View 。 单击提交按钮后,它会将我的搜索值传递给控件以过滤我的组。
一切都过滤得很好。然而,我希望在执行操作后获得的网址并未出现。我刚刚得到本地主机/
我想显示的是 localhost/mySearchValue
我的项目中的路由是这样设置的,如果我在本地主机后输入一个值,它就会像我的搜索按钮一样过滤组。
关于我需要做什么才能让我的搜索值显示在 URL 中,您有什么想法吗?
这是我的部分观点
@using (Html.BeginForm("List","Group"))
{
@Html.TextBox(name: "search")
<input type="submit" value="search" />
}
我的 Controller
public ViewResult List(string search, int page = 1)
{
if (search == "")
{
search = null;
}
GroupsListViewModel model = new GroupsListViewModel
{
Groups = repository.Groups
.Where(g => search == null || g.Tag == search || g.Tag2 == search)
.OrderBy(g => g.GroupId)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.Groups.Count()
},
CurrentSearch = search
};
更新
@Html.BeginForm("List","Group",FormMethod.Get)
帮助我获取如下 localhost/?search=test 所示的 url,但是调用 Controller 时未设置搜索,因此不会发生过滤。我用于搜索的 url 架构如下 localhost/test
这是我的路由信息
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(null,
"",
new {
controller = "Group", action = "List",
search = (string)null, page = 1
}
);
routes.MapRoute(null,
"Page{page}",
new { controller = "Group", action = "List", search = (string)null },
new { page = @"\d+" }
);
routes.MapRoute(null,
"{search}",
new { Controller = "Group", action = "List", page = 1 }
);
routes.MapRoute(null,
"{search}/Page{page}",
new { controller = "Group", action = "List" },
new { page = @"\d+" }
);
routes.MapRoute(null, "{controller}/{action}");
}
最佳答案
描述
只要我理解问题
默认表单方法是POST
,因此您需要将表单方法设置为GET
才能看到网址中的搜索字符串。
如果我不明白您想要什么,请告诉我(作为评论),以便为您提供更多帮助)
示例
@Html.BeginForm("List","Group",FormMethod.Get)
更多信息
关于html - 如何让我的搜索查询显示在 URL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305167/