3

I have boolean value in my model and I return this model to view. I want to control actionlink visibility with this value. I found two example like this:

First

@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}

Second

<li style="visibility: @Model.UserCanCreate">@Html.ActionLink("Create New", "Create")</li>

What is the best way to show/hide htmlHelpers?

Thanks.

Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • 1
    @DanielB: you mean the *first*, right? The 2nd writes the link to the page and just hides it. – Faust Aug 17 '12 at 11:08

2 Answers2

3

The first option is the correct one.

@if (Model.UserCanCreate)
{
    @Html.ActionLink("Create New", "Create")
}

Never rely on visibility in the browser, give the the user only what he is allowed to see.

Marc
  • 6,749
  • 9
  • 47
  • 78
1

The first one is better for your case. If you use second - there will be hidden link on the page to do something, but in first case there will not any info about possibility to do ("Create New", "Create").

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38