asp.net-mvc MVC3 如何禁用/启用 ActionLink
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9846845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC3 How to disable/enable ActionLink
提问by Benk
I have if condition and I want to disable or enable my actionLink button.
我有 if 条件,我想禁用或启用我的 actionLink 按钮。
How would I do it?
我该怎么做?
@Html.ActionLink("Delete", "Delete", new { id = @Model.Id})
@Html.ActionLink("Delete", "Delete", new { id = @Model.Id})
Thanks,
谢谢,
回答by nemesv
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?
为了使其跨浏览器工作:HTML 锚标记是否应该尊重禁用属性?
回答by John Prado
To disable a "a" tag you can do:
要禁用“a”标签,您可以执行以下操作:
@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { onclick = "javascript:return false;" })
Or you can use JQuery:
或者你可以使用 JQuery:
@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { class = "linkdisabled" })
CSS:
CSS:
.linkdisabled{
cursor:text;
}
JQuery:
查询:
$function(){
$(".linkdisabled").click(function(){
return false;
}
}
回答by Miroslav Holec
Maybe you can create your own UI of type MvcHtmlString
也许您可以创建自己的 MvcHtmlString 类型的 UI
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 MvcHtmlStringon Google.
但我从来没有尝试过。尝试在 Google 上查找有关MvcHtmlString 的信息。
回答by Krisi Suci
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:
有人可能会发现这很有用,我曾经通过将 @Html.ActionLink 转换为输入来解决类似的问题<input type="submit" id = "submit" />,然后使用 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>

