19

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
  • 58,060
  • 25
  • 98
  • 150
Benk
  • 1,284
  • 6
  • 33
  • 64

6 Answers6

29

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
  • 138,284
  • 16
  • 416
  • 359
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
  • 804
  • 8
  • 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
6

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
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,139
  • 25
  • 22
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
  • 129
  • 1
  • 4
0
@if(condition)
{
   @Html.ActionLink("Delete", null, null, new { @class = "btn btn-danger",@style= "cursor: not-allowed;", @onclick = "return false;" })
}
else
{
   @Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "btn btn-primary", @onclick = "return true;" })
}



@if(condition)
{
   @Html.ActionLink("Delete", null, null, new { href = "#",@class = "btn btn-danger",@style= "cursor: not-allowed;" })
}
else
{
   @Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "btn btn-primary" })
}
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney May 18 '23 at 17:42