asp.net-mvc-3 - 匿名类型成员声明符无效。匿名类型成员必须使用成员赋值、简单名称或成员访问进行声明

标签 asp.net-mvc-3 c#-4.0 razor

我正在尝试添加其他属性 数据图标到我的操作链接,但我收到以下错误:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.



作品:
@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
            new { @rel = "external", @id = "btnProfile" })

异常(exception):
@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
            new { @rel = "external", @id = "btnProfile", @data-icon = "gear" })

最佳答案

更新:从上面 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" },
    })

请参阅此重载:http://msdn.microsoft.com/en-us/library/dd504988.aspx

您使用的助手只是创建字典的一种方便方法,但无论如何在幕后创建了字典。

关于asp.net-mvc-3 - 匿名类型成员声明符无效。匿名类型成员必须使用成员赋值、简单名称或成员访问进行声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196776/

相关文章:

asp.net - 显示详细信息时出错(ObjectContext 实例已被释放)

javascript - ASP.NET MVC : 2d array is null after being passed from javascript to MVC controller action

c#-4.0 - 为什么泛型类型参数的 .NET 4 差异不适用于类?

c# - 带有 List<T> 键的字典

wpf - 如何将图片保存在 wpf 中的图像控件上?

jquery - 用户点击后如何渲染部分 View (使用 Razor )?

asp.net-mvc - 多态模型绑定(bind)

jquery - 在新选项卡/窗口中打开 href mailto 链接

c# - 如何避免重复内联条件来定义 Html.EditorFor() 的 htmlAttributes

image - 使用 ASP.NET MVC 3 和 Entity Framework 4.1 Code First 在 SQL CE 4.0 中存储图像时出错