35

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?

5 Answers 5

102

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;" });
Sign up to request clarification or add additional context in comments.

4 Comments

I'm glad you didn't.. I found this answer useful.
I must note, that this solution works even if i omit the Context.User.Identity.Name How would you explain the reason for this working to someone who has Java background?
@Lauri its just a function call but with a C# concept called an extension method.
note - style doesn't require the @ - that just escapes reserved words like class in C#
8

VB sample:

 @Html.ActionLink("Home", "Index", Nothing, New With {.style = "font-weight:bold;", .class = "someClass"})

Sample Css:

.someClass
{
    color: Green !important;
}

In my case, I found that I need the !important attribute to over ride the site.css a:link css class

2 Comments

You probably wouldn't nee the !important attribute if you just move the .someClass line below your a:link line in your site.css. You could also add it to your style attribute to override it. Generally, the browser applies the lowest item in the CSS file, then applies the style attribute. The least important stuff in the CSS file should be at the top, the most important should be at the bottom.
In this case, not true. There is a default css file, and then a site specific css file (not my architecture). In this specific case, the !important attribute is necessary. I agree with your assessment though.
4

Reviving an old question because it seems to appear at the top of search results.

I wanted to retain transition effects while still being able to style the actionlink so I came up with this solution.

  1. I wrapped the action link with a div that would contain the parent style:
<div class="parent-style-one">
      @Html.ActionLink("Homepage", "Home", "Home")
</div>
  1. Next I create the CSS for the div, this will be the parent css and will be inherited by the child elements such as the action link.
  .parent-style-one {
     /* your styles here */
  }
  1. Because all an action link is, is an element when broken down as html so you just need to target that element in your css selection:
  .parent-style-one a {
     text-decoration: none;
  }
  1. For transition effects I did this:
  .parent-style-one a:hover {
        text-decoration: underline;
        -webkit-transition-duration: 1.1s; /* Safari */
        transition-duration: 1.1s;         
  }

This way I only target the child elements of the div in this case the action link and still be able to apply transition effects.

1 Comment

Step 3: "Because all an action link is, is an element when broken down as html so you just need to target that element in your css selection" BINGO.... that was what I needed to read to make my life infinitely better. Thank you.
0

In my case I used it with h tag :

<h1 style="color:red;text-align:center">
    @Html.ActionLink("Create New", "Create")
</h1>

Comments

0

Parameters

 @Html.ActionLink("Update","CreateOrUpdate",new {Id=category.Id},new { @class = "btn btn-primary" })

without Parameters

@Html.ActionLink("Back","Index",new {},new {@class="btn btn-danger"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.