[英]How do I create the correct route values for this ActionLink?
SearchResults.aspx
的模型是PersonSearch
一个实例; 当新页面的请求到达时(GET请求),操作方法应该接受它并计算新结果。
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult SearchResults(PersonSearch search, int? page)
{
ViewData["Results"] = new PaginatedList<Person>(_searchService.FindPersons(search), page ?? 0, 1);
return View("SearchResults", search);
}
然后我必须生成上一个/下一个链接:
<%= Html.ActionLink("Next Page >", "SearchResults", routeValues) %>
如果我使用routeValues = ViewData.Model
我可以看到对象属性传递了地址,但我无法添加“page”参数。
它认为使用正确的值创建另一个对象更好,而不是使用(并可能改变当前的路由值):
<%=Html.ActionLink("Next Page >", "SearchResults", new {
search = this.Model,
page = 1 //or whatever
}) %>
您需要覆盖ToString()。
如果你正在使用Razor(我发现OP在四年前发明了Razor之前,但人们发现这可能是使用它)。
我能够通过使用内联的@helper方法获得一些工作。
@helper RunnerLink(PersonSearch model, int page)
{
var routeParms =new RouteValueDictionary(model.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(model, null)));
routeParms.Add("page", page.ToString());
routeParms.Add("Controller", "Property");
@Html.ActionLink("Search", "Index", routeParms)
}
用法很简单 -
@RunnerLink(myPersonSearchInstance, 1)
它不是最优雅的解决方案,但如果您想要将对象作为routeValue传递,但是您需要传递其他项目,例如Controller
, Area
或OPs案例page
,它可以很好地工作。
您需要使用RouteLink而不是ActionLink。 你的代码应该是这样的
@Html.RouteLink("Next", new {controller = "SearchResults", action = "Index", search=samevalue, page=1 })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.