2011-07-25 217 views
1

我使用的是带有剃须刀的asp.net mvc。我怎样才能隐藏只有管理员的链接?隐藏所有人的管理链接

public class MyViewModel 
{ 
    public bool IsAdmin { get; set; } 

    ... some other model properties 
} 

和您的视图中:

+0

你如何确定某人是否是管理员? –

回答

1

你可以在你的视图模型声明一个布尔属性

@if (Model.IsAdmin) 
{ 
    <!-- show the link that only administrators are supposed to see --> 
    @Html.ActionLink("Do something very special", "Bar") 
} 

过程中,控制器的动作里面和渲染这种观点,你会填充此视图模型:

[Authorize] 
public ActionResult Foo() 
{ 
    var model = new MyViewModel 
    { 
     IsAdmin = User.IsInRole("Admin") 
    }; 
    return View(model); 
} 

显然只有管理员可以在我的酒吧行动我nvoke也应使用Authorize属性进行装饰:

[Authorize(Roles = "Admin")] 
public ActionResult Bar() 
{ 
    ... 
}