我有这样一个动作方法:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Search(String filter, String value, Int32? page) {
var set = new List<Employee>();
switch(filter) {
case "by-name": {
set = this.repository.Get(
e => (e.LastName + " " + e.FirstName + " " + e.MiddleName) == value
).ToList();
break;
}
case "by-empn": {
set = this.repository.Get(
e => e.EmployeeNumber == value
).ToList();
break;
}
default: return RedirectToAction("Search", "Employee");
}
ViewBag.SearchedEmployees = set.Count();
return View(set.ToPagedList(page ?? 1, PageSize));
}
搜索视图是这样的:
@if(Model.Count > 0) {
foreach(var item in Model) {
Html.RenderPartial("Employee.Card", item);
}
@Html.PagedListPager(
Model,
page => Url.Action("Search", new { page = page }),
new PagedListRenderOptions {
LinkToFirstPageFormat = "<< Beginning",
LinkToPreviousPageFormat = "< Back",
LinkToNextPageFormat = "Forth >",
LinkToLastPageFormat = "End >>"
}
)
}
搜索表单显示为部分视图:
@using(Html.BeginForm("Search", "Employee", FormMethod.Get, new { @class = "search-form" }))
{
<p>
@Html.TextBox("value")
</p>
<p>
@Html.RadioButton("filter", "by-name", true) By name <br/>
@Html.RadioButton("filter", "by-empn") By empn <br/>
</p>
<p>
<input type="image" src="@Url.Content("~/Content/Images/Search.png")" />
</p>
}
问题:我有N
页面链接。当我尝试转到第二页时,我面临着无限循环的重定向。这就是我实施我的行动的方式 - 默认情况下被解雇。所以filter/value
价值观null
在第二次行动呼吁上?为什么?如何重构我的搜索操作?
另外我应该如何为这样的动作配置路由?
谢谢!
编辑
因此,搜索操作的路线应该如下所示:
routes.MapRoute(
null,
"{controller}/{action}/Page{page}/filter{filter}/val{value}",
new { controller = "Employee", action = "Search" }
);
?
编辑 2
所以接下来可以写:
page => Url.Action("Search", new { filter = ViewBag.SearchFilter, value = ViewBag.SearchValue, page = page }),
在控制器内部:
public ActionResult Search(String filter, String value, Int32? page) {
ViewBag.SearchFilter = filter;
ViewBag.SearchValue = value;
// ...
}
这是正确的吗?