I\'m working on an ASP.Net MVC 3 site. The _Layout master view contains a menu and I want to hide some of the items in the menu based on if you are logged in and what roles you
Create a partial view and return the view from the controller:
LayoutViewModel.cs:
public class LayoutViewModel
{ ...
public bool ShowAdmin { get; set; }
}
LayoutController.cs:
public PartialViewResult GetAdminMenu()
{
LayoutViewModel model = new LayoutViewModel();
model.ShowAdmin = userHasPermission("Admin"); // change the code here
return PartialView("_AdminMenu", model);
}
_AdminMenu.cshtml (partial view):
@model DelegatePortal.ViewModels.LayoutViewModel
@if (@Model.ShowAdmin)
{
}
_Layout.csthml (main view):
...
@Html.Action("GetAdminMenu", "Layout")