根据ViewBag值启用/禁用Html.ActionLink

时间:2018-07-18 12:05:55

标签: asp.net-mvc viewbag actionlink

ActionLinksview上有更多MVC project 我需要根据ViewBag值启用/禁用这些按钮。

我已在视图中添加了此代码

@{ 
   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)" })

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

1 个答案:

答案 0 :(得分:1)

看起来您可以通过添加disable类来处理禁用问题。为什么不选择它呢?

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

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