Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 How to disable/enable ActionLink

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,

like image 978
Benk Avatar asked Mar 23 '12 21:03

Benk


People also ask

How do I disable a link in HTML?

It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.

What is an ActionLink?

ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


2 Answers

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?

like image 192
nemesv Avatar answered Nov 04 '22 02:11

nemesv


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;
    }
}
like image 29
John Prado Avatar answered Nov 04 '22 03:11

John Prado