【问题标题】:Razor Html.ActionLink ParametersRazor Html.ActionLink 参数
【发布时间】:2012-07-25 10:03:15
【问题描述】:

我认为有以下代码:

@Html.ActionLink(item.Title,  "Articles/Details", New With {.id = item.ArticleId})

它会生成以下链接:

/博客/文章/详情/1

我希望它产生这个,而不是:

/文章/详情/1

我尝试弄乱参数,但我不确定如何让它做我想要的。我怎样才能做到这一点?谢谢。

【问题讨论】:

    标签: asp.net-mvc vb.net asp.net-mvc-3 razor


    【解决方案1】:

    使用这个overload

    public static MvcHtmlString ActionLink(
        this HtmlHelper htmlHelper,
        string linkText,
        string actionName,
        string controllerName,
        Object routeValues,
        Object htmlAttributes
    )
    

    所以你的代码可以写成这样

    @Html.ActionLink(item.Title, "Details","Article",
                      New With {.id = item.ArticleId},Nothing)
    

    检查 this msdn page 以查看所有可用的重载

    【讨论】:

    • 谢谢,这行得通。本例中的“无”是什么?请告诉我在哪里可以找到完整的参数列表,例如:Html.ActionLink( title, WhatThisParamIs, etc.)
    • @user1477388:这意味着我们将 Nothing 作为 HTMLAttributes 传递。如果你想传递一些自定义 HTML 属性(CSS 类),你可以用它替换 Nothing。检查我更新的答案。
    • 很好的答案,虽然我注意到如果我不通过“无”,它就不起作用。我必须记住这一点。
    • @user1477388:是的。因为该重载需要第 5 个参数。Nothing 意味着您必须编写 Nothing(VB.NET 版本的 null)
    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 2011-12-04
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多