Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access
Solution 1
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.
Solution 2
I think you use underscore like data_icon and it translates it
Solution 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.
Related videos on Youtube
Eugene
Updated on June 15, 2022Comments
-
Eugene almost 2 years
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" })
-
Alex about 12 yearsinstead of
@data-icon
use@data_icon
-
Eugene about 12 yearsgives the same exception
-
Gambitier almost 4 yearsDoes this answer your question? How to specify data attributes in razor, e.g., data-externalid="23151" on @this.Html.CheckBoxFor(...)
-
-
xdumaine over 11 yearsWhen I do this, it renders the link like:
<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">Log Off</a>
-
KeithFearnley about 6 yearsI 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? -
Ameerudheen.K over 3 yearsperfect answer.