Search code examples
cssasp.net-mvc-3razor

ASP.NET MVC3 Razor - Html.ActionLink style


I'm trying to set the style of an action link like so:

  <text><p>Signed in as @Html.ActionLink(Context.User.Identity.Name,"Index",new { Controller="Account", @style="text-transform:capitalize;" })</p>

I'd expect this to be rendered as

<p>Signed in as <a href="Index" style="text-transform:capitalize;">MyName</a></p>

However, what's generated is

<p>Signed in as <a href="/Account?style=text-transform%3Acapitalize%3B">MyName</a></p>

with the style appended to the url instead. What am I doing wrong?


Solution

  • Here's the signature.

    public static string ActionLink(this HtmlHelper htmlHelper, 
                                    string linkText,
                                    string actionName,
                                    string controllerName,
                                    object values, 
                                    object htmlAttributes)
    

    What you are doing is mixing the values and the htmlAttributes together. values are for URL routing.

    You might want to do this.

    @Html.ActionLink(Context.User.Identity.Name, "Index", "Account", null, 
         new { @style="text-transform:capitalize;" });