无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问声明匿名类型成员

Eug*_*ene 22 razor c#-4.0 asp.net-mvc-3

我正在尝试向我的Action Link 添加其他属性数据图标,但我收到以下错误:

无效的匿名类型成员声明符.必须使用成员分配,简单名称或成员访问声明匿名类型成员.

作品:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
            new { @rel = "external", @id = "btnProfile" })
Run Code Online (Sandbox Code Playgroud)

例外:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
            new { @rel = "external", @id = "btnProfile", @data-icon = "gear" })
Run Code Online (Sandbox Code Playgroud)

Pau*_*yng 27

更新:根据Xander上面的评论,使用data_icon = "gear"

您可以使用IDictionary<string, object>替代HTML属性的匿名对象:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }
    , new Dictionary<string, object>
    {
        { "rel", "external" }, 
        { "id", "btnProfile" },
        { "data-icon", "gear" },
    })
Run Code Online (Sandbox Code Playgroud)

看到这个重载:http://msdn.microsoft.com/en-us/library/dd504988.aspx

您正在使用的帮助程序只是创建字典的一种方便方法,但无论如何都会在后台创建字典.

  • 当我这样做时,它呈现如下链接:`<a comparer ="System.Collections.Generic.GenericEqualityComparer1 [System.String]"count ="1"keys ="System.Collections.Generic.Dictionary2 + KeyCollection [System. String,System.Object]"values ="System.Collections.Generic.Dictionary2 + ValueCollection [System.String,System.Object]"href ="/ Account/LogOff/1"class ="ui-link">注销</A>` (3认同)

小智 17

我认为你使用像data_icon这样的下划线并对其进行翻译