剃刀VB.NET中的自定义MVC HTMLHelpers

时间:2013-07-26 11:31:51

标签: asp.net-mvc vb.net razor html-helper

我有以下自定义助手:

        <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的新手,不太清楚我做错了什么。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

您将routeValues作为Object传递,而不是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

此外,在View中调用html帮助程序时,不需要指定参数名称。使用此:

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