Search code examples
asp.net-mvcactionlinkhtml.actionlink

html.action. Add a class and keep routevalues and htmlattributes


How do i add a class to the below link, whilst maintaining the "htmlAttributes" and "routeValues" (which i guess i need as it was part of the template??)

<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>

I have tried the following with no luck:

<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" }. new { @class = "btn navbar-btn js-login-btn" })</li>

Solution

  • You can put all the html attributes in a single annonymous object and pass that as the 5th parameter of the overload you are using.

    This should work

    @Html.ActionLink("Register", "Register", "Account",routeValues: null, 
           htmlAttributes: new { id = "registerLink" , @class = "btn navbar-btn js-login-btn" })