18

I have if condition and I want to disable or enable my actionLink button.

How would I do it?

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

Thanks,

tereško
  • 57,247
  • 24
  • 95
  • 149
Benk
  • 1,254
  • 6
  • 31
  • 63

5 Answers5

28

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

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

Community
  • 1
  • 1
nemesv
  • 135,824
  • 16
  • 405
  • 353
22

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;
    }
}
John Prado
  • 779
  • 7
  • 19
  • Hi, nice solution.. but is there any way to make the text non-click able. Now it appears as a link but its not going anywhere. So is there any way to change its appearance. Thanks – Scorpion Apr 05 '12 at 15:40
  • You can add a style attribute or use a css class to do that: – John Prado May 21 '12 at 18:45
3

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.

Miroslav Holec
  • 3,071
  • 23
  • 21
3

A little late to the party, but in case anyone else stumbles across this... If your project is bootstrapped, you can do this:

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { @class = "btn btn-default disabled" })

or

@{
    bool btnDisabled = // Logic here;
}

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { @class = "btn btn-default " + @btnDisabled ? "disabled" : "" })
  • Adding true/false to the @class didn't work for me. I tested it by adding string "disabled" or adding empty string when button supposed to be enabled and it worked fine. With "disabled" added it would be my preferred solution and I would upvote this answer. – Mariusz Feb 25 '21 at 15:27
  • 1
    @Mariusz I edited the second part of my answer, just didn't finish the thought when I was writing it. Is that what you were looking for? – Jason Tressler Feb 25 '21 at 20:10
  • Exactly. Thanks and upvoted your answer:-) – Mariusz Apr 13 '21 at 11:50
1

Someone might find this useful, I once solved a similar problem by turning @Html.ActionLink into an input <input type="submit" id = "submit" /> and then you make it work as a link using javascript:

<script>
    $(document).ready(function () {
        $('#submit').click(function () {
            if(condition){
               //sth (not working as a link)
            }
            else
            {
                window.location.href = "/home/thanks"; //working as a link
            }
        })
</script>
Krisi Suci
  • 119
  • 1
  • 4