ASP.NET MVC 检查视图内的角色
在我的视图中,我有一些管理链接,我想根据用户角色隐藏和显示这些链接,如何在视图中执行此操作,例如
<%= if(CHECK IF USER ROLE ADMIN) { %>
<div class="tools">
<ul>
<li class="edit"><%= Html.ActionLink("Edit", "Edit", new { id = Model.storyId }) %></li>
<li class="delete"><%= Html.ActionLink("Delete", "Delete", new { id = Model.storyId }) %></li>
</ul>
</div>
<%= } %>
In my View I have some admin links that I would like to hide and show based on user role how can do this inside the view e.g.
<%= if(CHECK IF USER ROLE ADMIN) { %>
<div class="tools">
<ul>
<li class="edit"><%= Html.ActionLink("Edit", "Edit", new { id = Model.storyId }) %></li>
<li class="delete"><%= Html.ActionLink("Delete", "Delete", new { id = Model.storyId }) %></li>
</ul>
</div>
<%= } %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然而,在我看来,这是一个糟糕的想法。最好让ViewData或Model代表视图要显示的内容,视图可以简单地检查视图数据。控制器基类或动作过滤器可以非常简单地重复使用这一点,并允许代码存在于一个地方。
However this is a terrible idea in my opinion. It is better to let the ViewData or Model represent what the view is to display, and the view can simply check the view data. A controller base class or an action filter can make repetitive use of this very simple and allow the code to exist in one place.
我同意大多数其他人的观点,即如果愿意的话,这些数据应该由控制器或其他业务服务“预先确定”提供,而视图只是尽可能地使用 html 标记和语言控制结构来“充实page”使用其他典型的网页构建工具,例如 jquery、css 等。
I agree with most others that this data should be provided "pre-determined", if you will, by the controller or other business services whereas the View simply uses, as much as possible, html markup and language control structures to "flesh out the page" using other typical web page building goodies such as jquery, css, etc. etc.