MVC3 How to disable/enable ActionLink

Maybe you can create your own UI of type MvcHtmlString

public static MvcHtmlString MyActionLink(this HtmlHelper helper, bool isClickable, string altText, RouteValueDictionary routeValues, object htmlAttributes = null) 
{
    // some logic with isClickale parameter here
    if(isClickable == false)
    {}

    return new MvcHtmlString(helper.ToHtmlString());
}

and use it in your View

@Html.MyActionLink( // some parameters here )

But I have never try it. Try find something about MvcHtmlString on Google.


To disable a "a" tag you can do:

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { onclick = "javascript:return false;" })

Or you can use JQuery:

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { class = "linkdisabled" })

CSS:

.linkdisabled{
   cursor:text;
}

JQuery:

$function(){
    $(".linkdisabled").click(function(){
        return false;
    }
}

If you know on the server side that the link is not available then just render a message that the action is not available:

@if(condition)
{
   @Html.ActionLink("Delete", "Delete", new { id = @Model.Id})
}
else
{
   <text>Action is not available</text>
}

Otherwise you can only disable a link with

  • CSS: Disable link using css
  • JS: How to enable or disable an anchor using jQuery?

To make it work cross-browser: Should the HTML Anchor Tag Honor the Disabled Attribute?