Home > DeveloperSection > Forums > How to use ternary operator in Razor
Sachin Singh
Sachin Singh

Total Post:26

Points:182
Posted on    March-26-2016 12:33 AM

 ASP.NET MVC C#  ASP.Net  HTML  ASP.NET MVC  HTML5  Entity Framework 
Ratings:


 1 Reply(s)
 332  View(s)
Rate this:
Hi Everyone,

I have a view and i want to use ternary operator on view with the folliowing condition:
I want to check authenticity of the user if they are authenticate then doing some other task until unless doing other task with the help of User.Identity.IsAuthenticated .
Can anyone help
Thank you.




Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    March-26-2016 8:33 AM

Hi Sachin,
You use this:


@if(User.Identity.IsAuthenticated) 
 {
 <a class="xxxxx">//Do your Stuff </a>
 }
else {
             <a class="yyyyy">//Do your Stuff </a> 
     }
or we can also write like this
<a class="@=User.Identity.IsAuthenticated ? "xxxx" : "yyyy";">//Do your Stuff</a>

Don't want to miss updates? Please click the below button!

Follow MindStick