I\'m trying to add additional attribute data-icon to my Action Link, but I\'m getting the error below:
Invalid anonymous type member
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.
I think you use underscore like data_icon and it translates it
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.
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.