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>
@Html.DisplayNameFor(model => model.Name)

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



, ,

  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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: