【问题标题】:Problem using @Ajax.ActionLink in MVC when passing the htmlAttributes传递 htmlAttributes 时在 MVC 中使用 @Ajax.ActionLink 时出现问题
【发布时间】:2020-07-14 12:57:57
【问题描述】:

当我在 .vbhtml 视图中使用 htmlAttributes 调用 @Ajax.ActionLink 时:

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

页面中的结果是这个:

<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>

但是如果我去掉htmlAttributes参数:

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

那么结果就是这个(效果不错):

<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>

baseController.PathParams 方法总是返回一个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

所以这不是问题。问题是我何时使用htmlAttributes

有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc vb.net razor asp.net-ajax actionlink


    【解决方案1】:

    我发现了问题,如果你想使用ActionLink 方法和routeValues 作为RouteValueDictionary 那么htmlAttributes 应该是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
    

    【讨论】:

    • 检查 other 重载,应该有一个带有 actionName 和 controllerName 的重载 - 参数的数量可能决定使用哪个重载,所以你可以使用你的第一个如果您包含控制器名称的参数,则为 sn-p。可能会有所不同,因为您使用的是命名参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多