Have a link Appear based on a condition MVC2
Is there a way for me to make a link appear on an MVC2 page based on a condition (to be exact, whether a user is in a role?)
UPDATE: some more information
Generally speaking I don't have anything yet... I was just wondering what the best way to go about doing this was...
I want the link to show up here in the site.Master
<ul id="menu">
<%
if(true){}
%>
<li><开发者_StackOverflow%: Html.ActionLink("Home", "Index", "Home")%></li>
<li><%: Html.ActionLink("View Your Populations", "PopulationInfo", "PatientACO")%></li>
</ul>
You will need to create a Model for each page view that has a UserIsInRole
property (or whatever), and then you just check it and show the link based on that.
<ul id="menu">
<%
if(true){}
%>
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<% if (Model != null && Model.UserIsInRole()) { %>
<li><%: Html.ActionLink("View Your Populations", "PopulationInfo", "PatientACO")%></li>
<% } %>
</ul>
EDIT:
ViewModel:
public class ViewModel
{
public bool UserIsInRole {get;set;}
}
Controller:
public ActionResult Action()
{
var viewModel = new ViewModel();
viewModel.UserIsInRole = User.IsInRole("Role");
return View(viewModel);
}
View:
<ul id="menu">
<% if (Model.UserIsInRole) {
<li><% Html.ActionLink("LinkText", "NewAction", "NewController") %></li>
<% } %>
</ul>
精彩评论