0

我有以下自定义助手:

        <Extension> _
    Public Function ActionLinkAuthorized(htmlHelper As HtmlHelper, linkText As String, actionName As String, controllerName As String, routeValues As RouteValueDictionary, htmlAttributes As IDictionary(Of String, Object)) As MvcHtmlString
        If (Roles.IsUserInRole("Administrator")) Then
            Return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes)
        Else
            Return MvcHtmlString.Empty
        End If
    End Function

这可以在我的剃刀视图中很好地看到,并且我尝试按如下方式使用它:

@Html.ActionLinkAuthorized("Edit", "Edit", "Account", New With {.id = currentItem.Id}, htmlAttributes:=New With {.class = "btn btn-warning", .title = "Edit"})

但是在运行我的应用程序时出现以下错误:

“(第 193 行)”类型的值无法转换为“System.Web.Routing.RouteValueDictionary”。

我对 VB.NET 完全陌生,不太确定我做错了什么。非常感激任何的帮助。

4

1 回答 1

0

您将 routeValues 作为对象传递,而不是作为 RouteValueDictionary。将您的 html 助手更改为:

<Extension> _
Public Function ActionLinkAuthorized(htmlHelper As HtmlHelper, linkText As String, actionName As String, controllerName As String, routeValues As Object, htmlAttributes As Object) As MvcHtmlString
    If (Roles.IsUserInRole("Administrator")) Then
        Return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes)
    Else
        Return MvcHtmlString.Empty
    End If
End Function

此外,在视图中调用 html 帮助程序时,您不需要指定参数名称。用这个:

@Html.ActionLinkAuthorized("Edit", "Edit", "Account", New With {.id = currentItem.Id}, New With {.class = "btn btn-warning", .title = "Edit"})
于 2013-07-27T03:18:44.443 回答