title: Conditionally add htmlAttributes to ASP.NET MVC Html.ActionLink
body: <p>I'm wondering if it's possible to conditionally add a parameter in a call to a method.</p>
  
  <p>For example, I am rendering a bunch of links (six total) for navigation in my Site.Master:</p>
  
  <pre><code>&lt;%= Html.ActionLink("About", "About", "Pages") %&gt; | 
  &lt;%= Html.ActionLink("Contact", "Contact", "Pages") %&gt;
  &lt;%-- etc, etc. --%&gt;
  </code></pre>
  
  <p>I'd like to include a CSS class of "selected" for the link if it's on that page. So in my controller I'm returning this:</p>
  
  <pre><code>ViewData.Add("CurrentPage", "About");
  return View();
  </code></pre>
  
  <p>And then in the view I have an htmlAttributes dictionary:</p>
  
  <pre><code>&lt;% Dictionary&lt;string,object&gt; htmlAttributes = new Dictionary&lt;string,object&gt;();
     htmlAttributes.Add("class","selected");%&gt;
  </code></pre>
  
  <p>Now my only question is how do I include the htmlAttributes for the proper ActionLink. I could do it this way for each link:</p>
  
  <pre><code>&lt;% htmlAttributes.Clear();
     if (ViewData["CurrentPage"] == "Contact") htmlAttributes.Add("class","selected");%&gt;
  &lt;%= Html.ActionLink("Contact", "Contact", "Pages", htmlAttributes) %&gt;
  </code></pre>
  
  <p>But that seems a little repetitive. Is there some way to do something like this psuedo code:</p>
  
  <pre><code>&lt;%= Html.ActionLink("Contact", "Contact", "Pages", if(ViewData["CurrentPage"] == "Contact") { htmlAttributes }) %&gt;
  </code></pre>
  
  <p>That's obviously not valid syntax, but is there a correct way to do that? I'm open to any totally different suggestions for rendering these links. I'd like to stay with something like ActionLink that takes advantage of using my routes though instead of hard coding the tag.</p>