2

I have an Asp.net application with razor view engine

I'd like to apply a css style in Html.ActionLink. So I tried to change this :

 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Espace propriétaire du terrain <span class="caret"></span></a>

To this

 @Html.ActionLink("Espace propriétaire du terrain ", "About", "Home",null, new {  @style="class:dropdown-toggle;data-toggle:dropdown; role:button; aria-expanded:false" })

But the style isn't applied and I get this as a source html code

<a href="/Home/About" style="class:dropdown-toggle;data-toggle:dropdown; role:button; aria-expanded:false">Espace propriétaire du terrain </a>

So I need to know :

  1. Why this happens?
  2. How can I fix my code?
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 1
    It needs to be `new { @class="dropdown-toggle", data_toggle="dropdown", role="button", aria_expanded="false" }` –  Apr 16 '15 at 12:46
  • possible duplicate of [How do I apply a CSS class to Html.ActionLink in ASP.NET MVC?](http://stackoverflow.com/questions/1444495/how-do-i-apply-a-css-class-to-html-actionlink-in-asp-net-mvc) – lt.kraken Apr 16 '15 at 12:46
  • Attributes.Add("style", "class = dropdown-toggle;"); not work? – Splunk Apr 16 '15 at 12:47
  • Possible duplicate, but with a quick look i can tell that "@style" means you're attempting to set the style attribute. In order to set the class attribute you'd have to use "@class" – lt.kraken Apr 16 '15 at 12:48
  • @larssy1 The style attribute would just be `style="[CSS HERE]"` – Luke Apr 16 '15 at 12:49

1 Answers1

6

Specify your HTML attributes as multiple values within your anonymous new {} object:

@Html.ActionLink("Espace propriétaire du terrain ", "About", "Home",null, new {  @class="dropdown", data_toggle="dropdown", role="button", aria_expanded = "false" })

In your example you are just adding and setting the value for the style attribute, when you want multiple attributes within the generated HTML tag.

NB:

  • The helper will automatically change your underscores to dashes for the attribute names.

  • The @ symbol should be prepended to any field names in the anonymous object that match exactly a reserved word in C#.

Luke
  • 22,826
  • 31
  • 110
  • 193