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

时间:2020-12-07 21:14:20

Using MVC 4

使用MVC 4

I have a partial view where I am creating a a search box. When the submit button is clicked it then passes the value of my search to the control to filter my groups.

我有一个部分视图,我正在创建一个搜索框。当单击submit按钮时,它会将搜索的值传递给控件以筛选我的组。

Everything filters just fine. However the url I am hoping to get after performing the action is not coming up. I just get localhost/

所有过滤器。但是,我希望在执行操作之后获得的url没有出现。我刚刚得到localhost /

What I would like to show up would be localhost/mySearchValue

我想要显示的是localhost/mySearchValue

The routing in my project is set up so that if I were to type a value after local host it would then filter the groups just as my search button does.

我的项目中的路由已经设置好了,如果我要在本地主机之后输入一个值,它就会像我的search按钮那样过滤组。

Any ideas on what I need to do to get my search value to show up in the URL?

有什么办法可以让我的搜索值显示在URL中?

Here is my partial view

这是我的部分观点。

@using (Html.BeginForm("List","Group"))
{ 
   @Html.TextBox(name: "search")
   <input type="submit"  value="search" />
}

My 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
        };

Update

更新

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

Helps me get a url as follows localhost/?search=test however search is not being set when the controller is called and so no filtering happens. My url schema for searching is as follows localhost/test

帮助我获取一个url,如下所示搜索=测试,但在调用控制器时没有设置搜索,因此不发生过滤。搜索的url模式如下所示

Here is my routing information

这是我的路由信息

    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}");
    }

1 个解决方案

#1


2  

Description

As long as i understand the question

只要我理解这个问题

Default form method is POST so you need set the form method to GET in order to see the search string in the url.

默认的表单方法是POST,所以需要设置表单方法来获取,以便查看url中的搜索字符串。

Please let me know (as a comment) if i dont understand what your want in order to help you more)

如果我不明白你想要什么来帮助你,请告诉我(作为评论)。

Sample

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

More Information

#1


2  

Description

As long as i understand the question

只要我理解这个问题

Default form method is POST so you need set the form method to GET in order to see the search string in the url.

默认的表单方法是POST,所以需要设置表单方法来获取,以便查看url中的搜索字符串。

Please let me know (as a comment) if i dont understand what your want in order to help you more)

如果我不明白你想要什么来帮助你,请告诉我(作为评论)。

Sample

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

More Information