2

使用 MVC 4

我有一个部分视图,我正在创建一个搜索框。单击提交按钮时,它将我的搜索值传递给控件以过滤我的组。

一切都过滤得很好。但是,我希望在执行操作后获得的 url 没有出现。我只是得到本地主机/

我想显示的是 localhost/mySearchValue

我的项目中的路由已设置,因此如果我要在本地主机之后键入一个值,它将像我的搜索按钮一样过滤组。

关于我需要做什么才能让我的搜索值显示在 URL 中的任何想法?

这是我的部分观点

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

我的控制器

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,但是在调用控制器时未设置搜索,因此不会发生过滤。我用于搜索的 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}");
    }
4

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.

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

于 2013-03-08T23:41:47.670 回答