使用MVC 4
我有一个部分视图,我在创建一个搜索框。 单击提交按钮后,它会将我的搜索值传递给控件以过滤我的组。
一切都过滤得很好。然而,我希望在执行动作后获得的URL不会出现。我刚收到localhost /
我想要显示的是localhost / mySearchValue
我的项目中的路由设置是这样的,如果我在本地主机之后键入一个值,它就会像我的搜索按钮那样过滤组。
我需要做些什么才能让我的搜索值显示在网址中?
这是我的部分视图
@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)
帮助我获取一个url,如下localhost /?search = test但是在调用控制器时没有设置搜索,因此没有进行过滤。我的搜索网址架构如下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}");
}
答案 0 :(得分:2)
只要我理解了这个问题
默认表单方法为POST
,因此您需要将表单方法设置为GET
,以便在网址中查看搜索字符串。
如果我不明白您的需求,请告诉我(作为评论),以便更多地帮助您。
@Html.BeginForm("List","Group",FormMethod.Get)