具有多个参数的 ActionLink

发布于 2024-10-30 21:09:15 字数 271 浏览 5 评论 0原文

我想用我的 ActionLink 创建一个像 /?name=Macbeth&year=2011 这样的 URL,我尝试这样做:

<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>

但它不起作用。我该怎么做?

I want to create a URL like /?name=Macbeth&year=2011 with my ActionLink which I have tried doing like so:

<%= Html.ActionLink("View Details", "Details", "Performances", new { name = item.show }, new { year = item.year })%>

but it doesn't work. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2024-11-06 21:09:15

您使用的重载使 year 值最终出现在链接的 html 属性中(检查您的渲染源)。

重载签名如下所示:

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

您需要将两个路由值放入 RouteValues 字典中,如下所示:

Html.ActionLink(
    "View Details", 
    "Details", 
    "Performances", 
    new { name = item.show, year = item.year }, 
    null
)

The overload you are using makes the year value end up in the html attributes of the link (check your rendered source).

The overload signature looks like this:

MvcHtmlString HtmlHelper.ActionLink(
    string linkText, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes
)

You need to put both your route values in to the RouteValues dictionary like this:

Html.ActionLink(
    "View Details", 
    "Details", 
    "Performances", 
    new { name = item.show, year = item.year }, 
    null
)
得不到的就毁灭 2024-11-06 21:09:15

除了 Mikael Östberg 答案之外,请在您的 global.asax 中添加类似的内容,

routes.MapRoute(
    "View Details",
    "Performances/Details/{name}/{year}",
    new {
        controller ="Performances",
        action="Details", 
        name=UrlParameter.Optional,
        year=UrlParameter.Optional
    });

然后在您的控制器中添加类似的内容

// the name of the parameter must match the global.asax route    
public action result Details(string name, int year)
{
    return View(); 
}

In addition to Mikael Östberg answer add something like this in your global.asax

routes.MapRoute(
    "View Details",
    "Performances/Details/{name}/{year}",
    new {
        controller ="Performances",
        action="Details", 
        name=UrlParameter.Optional,
        year=UrlParameter.Optional
    });

then in your controller

// the name of the parameter must match the global.asax route    
public action result Details(string name, int year)
{
    return View(); 
}
请远离我 2024-11-06 21:09:15

基于 Mikael Östberg 的回答,以防万一人们需要知道它如何处理 html attr。这是另一个示例,参考 ActionLink

@Html.ActionLink("View Details", 
"Details", 
"Performances", 
  new { name = item.show, year = item.year }, 
  new {@class="ui-btn-right", data_icon="gear"})


@Html.ActionLink("View Details", 
"Details", 
"Performances", new RouteValueDictionary(new {id = 1}),new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })

Based on Mikael Östberg answer and just in case people need to know how it does with html attr. Here is another example, reference from ActionLink

@Html.ActionLink("View Details", 
"Details", 
"Performances", 
  new { name = item.show, year = item.year }, 
  new {@class="ui-btn-right", data_icon="gear"})


@Html.ActionLink("View Details", 
"Details", 
"Performances", new RouteValueDictionary(new {id = 1}),new Dictionary<string, object> { { "class", "ui-btn-test" }, { "data-icon", "gear" } })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文