2013-07-26 75 views
0

我有以下的自定義幫助:定製MVC HTMLHelpers在剃刀VB.NET

 <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 

這可以看出,在我的Razor視圖精細和我嘗試按如下方式使用它:

@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的新手,不太清楚自己做錯了什麼。非常感激任何的幫助。

回答

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"})