1

I am new to MVC and razor. I have an MVC 4 application. In the Shared -> _Layout.cshtml page, I have some content that I would like to hide if the user in not in the allowed list. How should I proceed?

I tried using something like this, but when I look at the running code, the parts between the <% %> are commented out.

<div>
    <a href="../Home/Index" style="color: White;">Home</a>
    <% if(*a condition*) { %>
        <a href="../Admin/Index" style="color: White;">Admin</a>
    <% } %>
</div>

Thank you

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

1

If you are using razor, you just need to use @:

<div>
    <a href="../Home/Index" style="color: White;">Home</a>
    @if(1==2) {
        <a href="../Admin/Index" style="color: White;">Admin</a>
    }
</div>

However, I would suggest you also look at using ActionLink, instead of hard coding the controller routes

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • 1
    Thank you very much, the @ solved it. Indeed, ActionLink would be more appropriate. I have to rember to keep to razor, I used to be html and .net. – user1482939 Aug 16 '12 at 15:59
  • @user1482939 razor is a lovely, flowing syntax which allows you to bash out screens very quickly. – StuartLC Aug 16 '12 at 16:07