MVC ActionLink html attributes

Nick Kahn picture Nick Kahn · Jul 3, 2015 · Viewed 7k times · Source

I want to use ActionLink instead of plain html to popup my modal window but its working fine with plain html tag but not with MVC actionlink please have a look below.

from: (working)

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">

to: (error)

@Html.ActionLink("Edit", "Edit", null, new { id = @item.Id }, new { @data-toggle="modal", @data-target="#myModal" })

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

Answer

user3559349 picture user3559349 · Jul 3, 2015

You need to use the underscore character instead or the hyphen character in the attribute name (the html helper will output the html correctly). Note also the @ character is not required (its only necessary when using a reserved word, e.g. class or readonly)

@Html.ActionLink("Edit", "Edit", null, new { id = @item.Id }, new { data_toggle="modal", data_target="#myModal" })