25

I'm trying to add additional attribute data-icon to my Action Link, but I'm getting the error below:

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

Works:

@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" })
naveen
  • 53,448
  • 46
  • 161
  • 251
Eugene
  • 1,037
  • 2
  • 12
  • 19
  • 9
    instead of `@data-icon` use `@data_icon` – Alex Feb 08 '12 at 16:01
  • gives the same exception – Eugene Feb 08 '12 at 16:13
  • Does this answer your question? [How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)](https://stackoverflow.com/questions/9444805/how-to-specify-data-attributes-in-razor-e-g-data-externalid-23151-on-this) – Gambitier May 21 '20 at 03:03

4 Answers4

30

UPDATE: From Xander's comment above, use data_icon = "gear"

You can use an IDictionary<string, object> in place of the anonymous object for HTML attributes:

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

See this overload: http://msdn.microsoft.com/en-us/library/dd504988.aspx

The helper you are using is just a convenient method of creating the dictionary, but behind the scenes the dictionary is created anyway.

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
  • 3
    When I do this, it renders the link like: `Log Off` – xdumaine Aug 30 '12 at 22:04
  • I had this problem with a period/dot in my parameter (a class Filter with a member that I wanted to pass), I found the following worked... `@Html.ActionLink("Profile", "Details", new RouteValueDictionary{ {"filter.member", myvalue.ToString() } } )` Hope the formatting works - basically use RouteValueDictionary instead of the anonymous type? – Keith Fearnley Feb 28 '18 at 20:39
19

I think you use underscore like data_icon and it translates it

Richard Maxwell
  • 508
  • 5
  • 7
3

I just use the following

@using System.Web.Routing

@{
    RouteValueDictionary RouteValues = new RouteValueDictionary();

    RouteValues["id"] = 11;
    RouteValues[Some_Name] = Some_Value; //do this with as many name/value pairs 
                                         //as you like
}

@Html.ActionLink("Link Text", "Action", "Controller", RouteValues)

which I learnt from Jon's answer in this post.

I have mainly used this in my controllers to provide the route values for RedirectToAction() methods, but i don't see why it shouldn't work in your view, you will need to add a @using System.Web.Routing; though.

Community
  • 1
  • 1
Ben
  • 5,525
  • 8
  • 42
  • 66
0

Use "data_icon" instead of "data-icon".

        @Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
        new { @rel = "external", @id = "btnProfile", @data_icon = "gear" })

The _ will automatically be converted to - in the resulting markup.

Gambitier
  • 504
  • 6
  • 18