2017-02-21 38 views
0

我想基於會話值隱藏html元素,使用剃刀或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>

什麼建議嗎?

+0

通過JS不可能的。 –

+0

你有什麼應該可以正常工作。你確定'Session [「UserRole」]擁有你期望的價值嗎? –

+1

爲什麼不只是'if(Session [「UserRole」]。ToString()==「1」)' –

回答

1

嘗試下面的邏輯,它可以爲你工作

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

只需使用一個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>