asp.net-mvcvb.netrazorasp.net-ajaxactionlink

Problem using @Ajax.ActionLink in MVC when passing the htmlAttributes


When I call @Ajax.ActionLink in my .vbhtml view using the htmlAttributes:

    @Ajax.ActionLink("LinkText", 
                     "Action",
                     routeValues:=baseController.PathParams(New With {.id = Model.Icodciud}), 
                     ajaxOptions:=New AjaxOptions() With {.HttpMethod = "POST", .UpdateTargetId = "myPanel", .InsertionMode = InsertionMode.InsertBefore},
                     htmlAttributes:=New With {.class = "btn btn-primary"})

The result in the page is this one:

<a class="btn btn-primary" data-ajax="true" data-ajax-method="POST" data-ajax-mode="before" data-ajax-update="#myPanel" 
href="/Controller/Action?Count=2&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D">LinkText</a>

But if I remove the htmlAttributes parameter:

@Ajax.ActionLink("LinkText", 
                 "Action",
                 routeValues:=baseController.PathParams(New With {.id = Model.Id}), 
                 ajaxOptions:=New AjaxOptions() With {.HttpMethod = "POST", .UpdateTargetId = "myPanel", .InsertionMode = InsertionMode.InsertBefore})

Then the result is this one (it works good):

<a data-ajax="true" data-ajax-method="POST" data-ajax-mode="before" data-ajax-update="#myPanel" 
href="/Controller/Action?Id=1&Other=Params">LinkText</a>

The baseController.PathParams method always returns a RouteValueDictionary:

    Public Function PathParams(Optional params As Object = Nothing) As RouteValueDictionary

        Dim dictionary As IDictionary(Of String, Object) = New Dictionary(Of String, Object)()

     ... some code ...

        Return New RouteValueDictionary(dictionary)

    End Function

So it can't be the problem. The problem is when I use htmlAttributes or not.

Any ideas?


Solution

  • I found the problem, If you want yo use the ActionLink method with routeValues as RouteValueDictionary then htmlAttributes should be an IDictionary:

    Public Shared Function ActionLink(ajaxHelper As AjaxHelper, linkText As String, actionName As String, routeValues As Object, ajaxOptions As AjaxOptions, htmlAttributes As Object) As MvcHtmlString
    
    Public Shared Function ActionLink(ajaxHelper As AjaxHelper, linkText As String, actionName As String, routeValues As RouteValueDictionary, ajaxOptions As AjaxOptions, htmlAttributes As IDictionary(Of String, Object)) As MvcHtmlString