0

I am working with .cshtml pages. I want to show some html conditionally by getting value from Session variable. If i am using if else condition in cshtml pages it works, but i want to replace it with ternary operator.

Here is working Code: -

    @if (HttpContext.Current.Session["RequestCount"] != null)
     {
       if (HttpContext.Current.Session["RequestCount"].ToString() != "0")
          {
            <li class="nav-item"><a class="nav-link ripple" href="@Url.Action("Images", "Admin")"> <i class="icon-bell-ring" style="position:relative"><em>@HttpContext.Current.Session["RequestCount"].ToString() </em></i><span>Images Request</span> </a> </li> 
          }
       else
          {
            <li class="nav-item"><a class="nav-link ripple" href="@Url.Action("Images", "Admin")"> <i class="icon-bell-ring"></i> <span>Images Request</span> </a> </li>
          }
     }

Trying to use ternary operator : -

  <li class="nav-item"><a class="nav-link ripple" href="@Url.Action("Images","Admin")"> <i class="icon-bell-ring" style="position:relative">@HttpContext.Current.Session["RequestCount"].ToString) != "0" ?<em>@HttpContext.Current.Session["RequestCount"].ToString(): &nbsp; </em></i><span>Images Request</span> </a> </li>
Gobind Gupta
  • 203
  • 3
  • 13
  • First, what error are you getting? Second, It looks like you have an open `` tag in one branch of the ternary and the close in the second branch. Try formatting that line a little better and it might become obvious why it isn't working. Also, parenthesis might make it easier to indicate where the end of the ternary is supposed to be. – Becuzz Aug 31 '18 at 12:59
  • Thanks @Becuzz, error is that it is not working conditionally, it shows both html. http://prntscr.com/kp3g0j – Gobind Gupta Aug 31 '18 at 13:31
  • I want to hide the red bubble if session has value == "0". – Gobind Gupta Aug 31 '18 at 13:37

1 Answers1

4

If you want to use the ternary operator, you need to do a couple of things.

  1. Surround the whole thing with parenthesis. As you have it written, the ? gets interpreted as text, not an operator. So start with something like this:

    @(myCondition ? "a" : "b")

  2. Don't put opening tags inside the operator (unless you put the close one in as well). So move the em tags outside like this.

    <em>@(/* ternary operator here */)</em>

  3. Finally, make sure the return type is the same for both branches. In your example you are trying to return a regular string in one part (the HttpContext bit) and the second you are trying to return a non-breaking space (I assume you don't want the literal text &nbsp; output to the page). So wrap them both in HtmlStrings.

So when it's all put together you get something like this (below is a sample Razor page in a .Net Core web app I tried this on, adapt to your needs):

@using Microsoft.AspNetCore.Html
@{
    bool isTrue = false;
}
<!DOCTYPE html>

<html>
<head>
    <title>title</title>
</head>
<body>
<div>
    <em>@(isTrue ? new HtmlString("hi") : new HtmlString("&nbsp;")) </em>
</div>
</body>
</html>
Becuzz
  • 6,846
  • 26
  • 39