简体   繁体   English

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

[英]Custom MVC HTMLHelpers in razor VB.NET

I have the following custom 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

This can be seen fine in my razor view and am attempting to use it as follows: 在我的剃刀视图中可以很好地看到这一点,并尝试按以下方式使用它:

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

But I am getting the following error when running my application: 但是运行应用程序时出现以下错误:

Value of type ' (line 193)' cannot be converted to 'System.Web.Routing.RouteValueDictionary'. 类型“(行193)”的值不能转换为“ System.Web.Routing.RouteValueDictionary”。

I'm completely new to VB.NET and not quite sure what I'm doing wrong. 我是VB.NET的新手,不太确定自己在做什么错。 Any help would be most appreciated. 非常感激任何的帮助。

You are passing the routeValues as an Object, not as a RouteValueDictionary. 您将routeValues作为对象而不是RouteValueDictionary传递。 Change your html helper to this: 将您的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

Also, you don't need to specify the argument name, when calling the html helper in your View. 另外,在视图中调用html helper时,无需指定参数名称。 Use this: 用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM