title: change Layout based on type of User role
body: <p>I am new to MVC, I looked through similar posts on stack, but they did'nt seem to help my problem. I have two roles Admin and User. When a person logs in, I want the layout to change based on whether the person had <em>Admin</em> privileges or <em>User</em> Privileges.</p>
  
  <p>My <em>Admin</em> Layout is <code>_Layout.cshtml</code></p>
  
  <p>My <em>User</em> Layout is <code>_LayoutUser.cshtml</code></p>
  
  <p>Is there something in my Login Controller that I can put which will change the layout based on the role of the User? The role of the user is specified in the Login table.</p>
  
  <pre><code>id     LoginStudentNumber    LoginPassword      Role
  1           123456              123456          Admin
  2           234567              234567          Admin
  3           345678              345678          User
  4           456789              456789          User
  </code></pre>
  
  <p>Login Controller:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using RMS.DAL;
  using RMS.Models;
  
  namespace RMS.Controllers
  {
      public class LoginController : Controller
      {
          RMSContext db = new RMSContext();
  
  
          [AllowAnonymous]
          public ActionResult Login(string returnUrl)
          {
              ViewBag.ReturnUrl = returnUrl;
              return View();
          }
  
          //
          // POST: /Account/Login
          [HttpPost]
          [AllowAnonymous]
          public ActionResult Login(Login model, int? LoginStudentNumber, string LoginPassword)
          {
  
  
              //RMS.Models.Login user = db.Logins.Find(LoginID);
              RMS.Models.Login user = db.Logins.FirstOrDefault(l =&gt; l.LoginStudentNumber == model.LoginStudentNumber);
  
  
              if (user == null)
              {
                 ViewBag.InvalidID = "Invalid User Id";
  
                  return View(model);
              }
  
  
  
              if ((user.LoginPassword.Equals(LoginPassword)) &amp;&amp; (user.Role.Equals("Admin")))
  
              {
  
                  return RedirectToAction("Index", "Home");
  
  
              }
  
              if ((user.LoginPassword.Equals(LoginPassword)) &amp;&amp; (user.Role.Equals("User")))
              {
  
                  return RedirectToAction("IndexUser", "Home");
  
  
              }
  
  
              ViewBag.InvalidPassword = "Invalid Password";
              return View(model);
  
          }
      }
  }
  </code></pre>
  
  <p><code>_Layout.cshtml</code> Page:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css"&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;@ViewBag.Title - Wits RMS&lt;/title&gt;
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
  &lt;/head&gt;
  &lt;body onload=display_ct();&gt;
      &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
          &lt;div class="container"&gt;
              &lt;div class="navbar-header"&gt;
                  &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;a href="@Url.Action("Index", "Home")"&gt;
                      &lt;img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" /&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="navbar-collapse collapse"&gt;
                  &lt;ul class="nav navbar-nav"&gt;
                      &lt;li&gt;@Html.ActionLink("Researcher", "Index", "Researcher")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Submission", "Index", "Submission")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Publication", "Index", "Publication")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Faculty", "Index", "Faculty")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("School", "Index", "School")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Contact", "Contact", "Home")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("About", "About", "Home")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Reports", "Index", "Reports")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Logout", "Login", "Login")&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container body-content"&gt;
          @RenderBody()
          &lt;hr /&gt;
          &lt;footer&gt;            
              &lt;script type="text/javascript"&gt;
                  function display_c() {
                      var refresh = 1000; // Refresh rate in milli seconds
                      mytime = setTimeout('display_ct()', refresh)
                  }
  
                  function display_ct() {
                      var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
                      ];
                      var dayOfWeek = ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
                      var strcount
                      var x = new Date()
                      var x1 = dayOfWeek [x.getDay()] + ", " + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                      x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                      document.getElementById('ct').innerHTML = x1
                      tt = display_c();
                  }
              &lt;/script&gt;
  
              &lt;p&gt;&amp;copy; @DateTime.Now.Year - Wits RMS &lt;span style="float:right; top:20px;" id='ct'&gt;&lt;/span&gt;&lt;/p&gt;                             
          &lt;/footer&gt;
      &lt;/div&gt;
      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/bootstrap")
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><code>_LayoutUser.cshtml</code> Page:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;@ViewBag.Title - Wits RMS&lt;/title&gt;
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
  &lt;/head&gt;
  &lt;body onload=display_ct();&gt;
      &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
          &lt;div class="container"&gt;
              &lt;div class="navbar-header"&gt;
                  &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;a href="@Url.Action("IndexUser", "Home")"&gt;
                      &lt;img src="~/Icons/Wits---Logo---Mono-Neg---Colloquial---Ext.png" style="height:45px; width:80px; margin-right:5px;" alt="" /&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="navbar-collapse collapse"&gt;
                  &lt;ul class="nav navbar-nav"&gt;                    
                      &lt;li&gt;@Html.ActionLink("Submission", "IndexUser", "Submission")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Publication", "Index", "Publication")&lt;/li&gt;                    
                      &lt;li&gt;@Html.ActionLink("Contact", "ContactUser", "Home")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("About", "AboutUser", "Home")&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Logout", "Login", "Login")&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="container body-content"&gt;
          @RenderBody()
          &lt;hr /&gt;
          &lt;footer&gt;
              &lt;script type="text/javascript"&gt;
                  function display_c() {
                      var refresh = 1000; // Refresh rate in milli seconds
                      mytime = setTimeout('display_ct()', refresh)
                  }
  
                  function display_ct() {
                      var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
                      ];
                      var dayOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
                      var strcount
                      var x = new Date()
                      var x1 = dayOfWeek[x.getDay()] + ", " + x.getDate() + " " + monthNames[x.getMonth()] + " " + x.getFullYear();
                      x1 = x1 + " " + ('0' + x.getHours()).slice(-2) + ":" + ('0' + x.getMinutes()).slice(-2) + ":" + ('0' + x.getSeconds()).slice(-2);
                      document.getElementById('ct').innerHTML = x1
                      tt = display_c();
                  }
              &lt;/script&gt;
  
              &lt;p&gt;&amp;copy; @DateTime.Now.Year - Wits RMS &lt;span style="float:right; top:20px;" id='ct'&gt;&lt;/span&gt;&lt;/p&gt;
          &lt;/footer&gt;
      &lt;/div&gt;
      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/bootstrap")
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>