Enable/Disable Html.ActionLink 基于 ViewBag 值

Enable/Disable Html.ActionLink based on a ViewBag value

我在 viewMVC project 上还有更多 ActionLinks 我需要根据 ViewBag 值 enable/disable 这些按钮。

我已经在视图中添加了这段代码

@{ 
   if (ViewBag.ModifyOS)
   {
      @Html.ActionLink(@Resources.Common.ModifyOSTerms, "ModifyOSTerms", "Document", new { lotId = LotId }, new { @class = "btn btn-success disabled" })
    }
     else
     {
       @Html.ActionLink(@Resources.Common.ModifyOSTerms, "ModifyOSTerms", "Document", new { lotId = LotId }, new { @class = "btn btn-success" })
      }
}

这种方式工作正常,但我想要一种更优雅的方式来实现这一点。 我试过这种方式:

@{
   string Disabled = "";
   if (ViewBag.ModifyOS)
   {
    Disabled = "disable";
   }   
  }

并且我已将此 Disabled 设置为 ActionLink

 @Html.ActionLink(@Resources.Common.ModifyOSTerms, "ModifyOSTerms", "Document", new { lotId = LotId }, new { @class = "btn btn-success" , disabled = "@Disabled" })

我试过这种方法:

@Html.ActionLink(@Resources.Common.ModifyOSTerms, "ModifyOSTerms", "Document", new { lotId = LotId }, new { @class = "btn btn-success" , disabled = "@(ViewBag.ModifyOS)" })

但在这两种情况下,按钮始终处于禁用状态。

看起来您可以通过添加 disable class 来处理禁用。为什么不去呢?

string cssClass = "btn btn-success";
if (ViewBag.ModifyOS)
{
    cssClass += " disabled";
}

@Html.ActionLink(@Resources.Common.ModifyOSTerms, "ModifyOSTerms", "Document", new { lotId = LotId }, new { @class = @cssClass })