Reputation: 1352
I m working on a MVC 3 application , it contain different links , i want to show the links according to the roles or rights. If Link A is to Admin thn Link A shouldnt be visble to Members right users.
How to achieve this thing in MVC ?
Thanks
Upvotes: 1
Views: 467
Reputation: 1939
if you're not using the asp membership, you could do it this way, supposing you could get the role by using the username and that the role is included in the model:
@{
UserContext userDb = new UserContext();
var user = userDb.UserModels.FirstOrDefault(x => x.Username.Equals(User.Identity.Name));
if(user.Role == "Admin")
{
@ActionLink("Link's Name","SomeAction", "SomeController");
}
}
crude sample but if that what you're looking for.
Upvotes: 1
Reputation: 28608
@if (User.IsInRole("Administrator")) {
@Html.ActionLink("Administration", "", "Admin")
}
Upvotes: 3