After logging into the site, the user has this menu:

enter image description here

It is necessary that some users - with logins: 0336 0019 0020 have one more additional menu Мониторинг :

enter image description here

Probably here it is necessary to use some already known mechanism or otherwise say the pattern. But I'm trying to do this through the ViewModel for _Layout:

ViewModelBase.cs

 public class ViewModelBase { public SchoolID { get; set; } public string[] monit10 = { "0336", "0019", "0020" }; } 

HomeController.cs

 public ActionResult Index() { if(!Request.IsAuthenticated) { return View("Index", "~/Views/Shared/_GuestLayout.cshtml"); } return View(new ViewModelBase { SchoolID = User.Identity.Name }); } 

_Layout.cshtml

 @model Monit95App.Models.ViewModelBase <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title – приложение ASP.NET</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") <script src="../Scripts/jquery-2.2.2.min.js" type="text/javascript"></script> </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("Главная", "Index", "Home", null, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> @if (Request.IsAuthenticated) //Если авторизация прошла успешно { <li>@Html.ActionLink("ИТОГОВЫЕ РАБОТЫ В 1-3 КЛАССАХ", "Select", "Work201615")</li> } @if (Model.monit10.Contains(Model.SchoolID)) //здесь я проверяю, входит ли логин в соответствующий список { <li> <a class="dropdown-toggle" data-toggle="dropdown" href="#">Мониторинг<span class="caret"></span></a> <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu"> <li class="dropdown-submenu"> <a tabindex="-1" href="#">Мониторинг 10 классов</a> <ul class="dropdown-menu"> <li>@Html.ActionLink("Общий план", "MainWindow", "Home")</li> <li>@Html.ActionLink("Список обучающихся", "GetLearnesList", "Monit10_1516")</li> </ul> </li> </ul> </li> } @*<li>@Html.ActionLink("Контакты", "Contact", "Home")</li>*@ @*Контакты школы*@ <li>@Html.ActionLink("О сервисе", "About", "Home")</li> </ul> @Html.Partial("_LoginPartial") </div> </div> </div> <div class="container body-content"> @RenderBody() </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html> 

The fact is that all this works after authorization or when clicking on the "Home" button. If I click another menu, for example, ИТОГОВЫЕ РАБОТЫ В 1-3 КЛАССАХ , then on this page the Monitoring menu will disappear.

That is, as I understand, for such users to constantly have the “Monitoring” menu, I need to pass the user name to the View in all the controller methods by:

  return View(new ViewModelBase { SchoolID = User.Identity.Name }); 

How to design an application for such a task. After all, I still need to make a separate View_ for the administrator’s work (for the administrator’s login)?

    1 answer 1

    Do not forget that User is available to you in the layout directly, it is not necessary to pass it through the model!

    With the list here, of course, it is more difficult - probably the easiest way is to put it directly into the file - if, of course, the check only happens in one place. Otherwise, a static field will do (as I understand it, it should not change right now).

     @if (new [] { "0336", "0019", "0020" }.Contains(User.Identity.Name)) { ... } 

    The most accurate way is to check the role (for this you need to make a role for monitoring users):

     @if (User.IsInRole("monit10")) { ... }