Hiding a link in ASP.NET MVC 4

A quick tip about visibility for y’all just starting out with MVC. I wanted to hide the “Delete” link on my view from all users who did not create the item being viewed. I was using the default authentication / authorization code that comes with MVC 4. I compared the current Identity name of the user to the user who created the item being viewed, showing the link if they were authorized to delete it:

@model IEnumerable<Park>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
    @if (User.Identity.Name == item.CreatedByUser)
         { @Html.ActionLink(“Delete”, “Delete”, new { id=item.ParkID }) }
</td>
</tr>
}

</table>
}

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: