【问题标题】:How to hide html.actionlink button given a condition如何在给定条件下隐藏 html.actionlink 按钮
【发布时间】:2017-05-30 07:14:56
【问题描述】:
<tbody>
    @foreach (var item in Model.Roles)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @foreach (var permission in item.Permissions)
                {
                    @Html.DisplayFor(modelItem => permission)
                    <br/>
                }

           </td>
           <td>
                        @Html.ActionLink("Edit", "EditRole", > "Roles", new { RoleID = item.RoleID}, null) // i want when RoleID = 1, it will > hide the button
                @{ if (item.RoleID == 1) // this is the condition
                    {
                        @:style = "display:none"
                    }
                }
            </td>
        </tr>
    }
</tbody>

【问题讨论】:

  • 下次请尝试更好地格式化您的代码。另外,为什么不简单地将Html.ActionLink() 放在您的 if 语句中?
  • 你可以像这样给 ActionLink 一些 ID - 并在你的条件下使用 JQ 隐藏

标签: c# visual-studio razor


【解决方案1】:

您可以这样做,通过 ActionLink 参数在您的 HTML 属性上声明样式属性:

@if (item.RoleID == 1)
{
    @Html.ActionLink("Edit", "EditRole", "Roles", new { RoleID = item.RoleID}, null) 
}
else
{
   @Html.ActionLink("Edit", "EditRole", "Roles", new { RoleID = item.RoleID }, new { @style = "display:none" })
}

【讨论】:

    【解决方案2】:

    你好,你可以用这个

    @if (item.RoleID == 1) { 
       @Html.ActionLink(item.marketGroupName, "Index", new { id = 
      item.marketGroupID });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多