11

How would you use an Html.ActionLink to render the following link -

<a href="javascript:void(0);"></a>

It may seem silly to do this, but sometimes I need a link that has link functionality (rollover pointers, etc.) but does not go anywhere. And I want to use an Html.ActionLink for code consistency.

I have tried different variations of Html.ActionLink but I keep getting messages about things not allowed to be null.

4
  • 3
    If that's all you need in the link, why not just use exactly that? Skip Html.ActionLink all together. Is there something specifc you need from Html.ActionLink?
    – Leniency
    Feb 12, 2013 at 19:29
  • agreed. just use an anchor Feb 12, 2013 at 19:29
  • I have tried different variations of Html.ActionLink but I keep getting messages about things not allowed to be null. I want to use an Html.ActionLink for code consistancy, and I want a link to exist, with anything happening when it's clicked. Thanks!
    – A Bogus
    Feb 12, 2013 at 19:47
  • ActionLink produces a link to an action as the name suggests. You don't want a link to an action, so ActionLink isn't the right tool. You could create your own helper method if you need this functionality often. Feb 12, 2013 at 20:15

2 Answers 2

15
@Html.ActionLink(" ", "", "", new {href="javascript:void(0)"})

will render as

<a href="javascript:void(0)"> </a>
3
  • Thanks Joe. But my problem is I want a no "Link Text" and your solution gives me Value cannot be null or empty. Parameter name: linkText
    – A Bogus
    Feb 12, 2013 at 20:14
  • 1
    Will just a space work? Otherwise we can try a custom helper method. Out of curiosity how are the rollover pointers triggered if there is no content to roll-over?
    – Joe
    Feb 12, 2013 at 20:21
  • A blank space worked! Thanks! Sorry for my dopiness. To answer your question, the link is in a div that has a background image.
    – A Bogus
    Feb 12, 2013 at 20:59
6

Instead of forcing ActionLink to do something it isn't made for, consider creating your own helper method:

public static class MyHtmlExtensions
{
    public static MvcHtmlString EmptyLink(this HtmlHelper helper, string linkText)
    {
        var tag = new TagBuilder("a");
        tag.MergeAttribute("href", "javascript:void(0);");
        tag.SetInnerText(linkText);
        return MvcHtmlString.Create(tag.ToString());
    }
}

Import the namespace into your view and you'll be able to do this:

@Html.EmptyLink("My link text")

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.