【问题标题】:How to hide HTML element based on Session value如何根据会话值隐藏 HTML 元素
【发布时间】:2017-02-21 11:57:48
【问题描述】:

我正在尝试使用 razor 或 js 隐藏基于会话值的 html 元素,但我不能。我尝试了不止一种解决方案,但没有结果。

第一次尝试:

<div class="btn-group">
  <a href="#" class="btn btn-default">الخيارات</a>
  <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <span class="caret"></span> 
  </a>
  <ul class="dropdown-menu">
    <li style="@(Session[" UserRole "].Equals("2 ") ? "display:none " : "display:block ")">
      <a href="@Url.Action(" ConfirmAllNotes ", "Notes ", new { ReportID = item.ReportID })"></a>اعتماد الملاحظات
    </li>
    <li>
      <a href="@Url.Action(" DepartmentResponse ", "Notes ", new { ReportID =item.ReportID })">ردود الإدارة</a>
    </li>
    <li>
      <a href="@Url.Action(" Edit ", "Reports ", new {id = item.ReportID })">تعديل</a>
    </li>
  </ul>
</div>

第二次尝试:

<div class="btn-group">
  <a href="#" class="btn btn-default">الخيارات</a>
  <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <span class="caret"></span>
  </a>
  
  <ul class="dropdown-menu">
    @if (Session["UserRole"].ToString().Equals("1")) {
      <li>
        <a href="@Url.Action(" ConfirmAllNotes ", "Notes ", new { ReportID = item.ReportID })"></a>اعتماد الملاحظات
      </li>
    }
    <li>
      <a href="@Url.Action(" DepartmentResponse ", "Notes ", new { ReportID =item.ReportID })">ردود الإدارة</a>
    </li>
    <li>
      <a href="@Url.Action(" Edit ", "Reports ", new {id = item.ReportID })">تعديل</a>
    </li>
  </ul>
</div>

有什么建议吗?

【问题讨论】:

  • 无法通过js。
  • 您所拥有的应该可以正常工作。你确定Session["UserRole"] 持有你期望的值吗?
  • 为什么不只是if(Session["UserRole"].ToString() == "1")
  • @RoryMcCrossan 是的,它拥有我需要的价值。我在其他情况下使用它,但我不确定这是否是最佳做法。
  • @CarstenLøvboAndersen 谢谢你,它有效。

标签: javascript c# jquery asp.net-mvc razor


【解决方案1】:

试试下面的逻辑,它可能对你有用

   <li style="@(Session["UserRole"].ToString() == "2" ? "display:block" : "display:none")">

【讨论】:

    【解决方案2】:

    只需使用if,而不是与元素的样式混合:

    <div class="btn-group">
      <a href="#" class="btn btn-default">الخيارات</a>
      <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <span class="caret"></span> 
      </a>
      <ul class="dropdown-menu">
        @if (Session[" UserRole "].Equals("2 "))
        {
        <li>
          <a href="@Url.Action(" ConfirmAllNotes ", "Notes ", new { ReportID = item.ReportID })"></a>اعتماد الملاحظات
        </li>
        }
        <li>
          <a href="@Url.Action(" DepartmentResponse ", "Notes ", new { ReportID =item.ReportID })">ردود الإدارة</a>
        </li>
        <li>
          <a href="@Url.Action(" Edit ", "Reports ", new {id = item.ReportID })">تعديل</a>
        </li>
      </ul>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多