gpt4 book ai didi

html - 如何让我的搜索查询显示在 URL 中?

转载 作者:行者123 更新时间:2023-11-28 00:10:45 24 4
gpt4 key购买 nike

使用 MVC 4

我有一个局部 View ,我正在其中创建一个搜索框。单击提交按钮后,它会将我的搜索值传递给控件以过滤我的组。

一切都过滤得很好。但是我希望在执行操作后获得的 url 没有出现。我刚得到 localhost/

我想显示的是 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 才能在 url 中看到搜索字符串。

如果我不明白您想要什么以便为您提供更多帮助,请告诉我(作为评论)

示例

@Html.BeginForm("List","Group",FormMethod.Get)

更多信息

关于html - 如何让我的搜索查询显示在 URL 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305167/

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