C# 如何为此 ActionLink 创建正确的路由值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1810063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:03:06  来源:igfitidea点击:

How do I create the correct route values for this ActionLink?

c#asp.net-mvcactionlinkroutevalues

提问by gremo

The Model of SearchResults.aspxis an instance of PersonSearch; when the request for a new page arrive (a GET request), the action method should take it and compute the new results.

的模型SearchResults.aspx是 的实例PersonSearch;当对新页面的请求到达时(GET 请求),action 方法应该接受它并计算新结果。

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

Then I have to generate the previous/next links:

然后我必须生成上一个/下一个链接:

<%= Html.ActionLink("Next Page >", "SearchResults", routeValues) %>

If I use routeValues = ViewData.ModelI can see the object properties passed the address, but I can't add the "page" parameter.

如果我使用,routeValues = ViewData.Model我可以看到传递地址的对象属性,但我无法添加“页面”参数。

回答by LorenzCK

It think it would be better to create another object with the correct values, instead of using (and potentially altering the current routevalues):

它认为最好用正确的值创建另一个对象,而不是使用(并可能改变当前的路由值):

<%=Html.ActionLink("Next Page >", "SearchResults", new {
    search = this.Model,
    page = 1 //or whatever
}) %>

回答by LorenzCK

You need to override ToString().

您需要覆盖 ToString()。

回答by daniellmb

This blog post by Scott Guthrie helped me wrap my head around URL Routing: ASP.NET MVC Framework (Part 2): URL Routing

Scott Guthrie 的这篇博文帮助我了解了 URL 路由:ASP.NET MVC 框架(第 2 部分):URL 路由

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

I love that he included test cases! enter image description here

我喜欢他包含测试用例! 在此处输入图片说明

回答by Nate

If you are using Razor (I realize OP asked four years ago before Razor was invented, but people finding this maybe using it).

如果您正在使用 Razor(我意识到 OP 在 Razor 发明之前就在四年前问过,但人们发现它可能会使用它)。

I was able to get something working by using an inline @helper method.

我能够通过使用内联 @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)
}

Usage would be simple --

用法很简单——

@RunnerLink(myPersonSearchInstance, 1)

It isn't the most elegant solution, but it works well if you want to pass an object in as a routeValue, but you need to pass additional items, such as Controller, Areaor in OPs case page.

这不是最优雅的解决方案,但如果您想将对象作为 routeValue 传入,它会运行良好,但您需要传递其他项目,例如Controller,Area或在 OPs case 中page

回答by Дмитрий Кононов

You need use RouteLink instead ActionLink. Your code should look something like this

您需要使用 RouteLink 而不是 ActionLink。你的代码应该是这样的

@Html.RouteLink("Next", new {controller = "SearchResults", action = "Index", search=samevalue, page=1 })