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,
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.
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.
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?
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With