title: Including the login form within the master layout page - ASP.NET MVC 3
body: <p>I'm trying to include my log-in form in the _Layout.cshtml file, but I'm having a few problems with it.</p>
  
  <p>First of all, when I provide an incorrect username and/or password, or the input fields don't pass validation, instead of displaying the error messages in the partial view, it makes the partial view take up the entire screen!</p>
  
  <p>This is the partial view: <a href="http://i.imgur.com/oisCj85.png?1" rel="nofollow">http://i.imgur.com/oisCj85.png?1</a></p>
  
  <p>This is the 'partial' view returned when something goes wrong with logging in: <a href="http://i.imgur.com/Uc5Sh2t.png?1" rel="nofollow">http://i.imgur.com/Uc5Sh2t.png?1</a></p>
  
  <p>And secondly, when the log-in <em>is</em> successful, the user does get logged-in, the page 'reloads', but the Javascript in <code>&lt;body onload="loadPage();&gt;</code> in _Layout.cshtml does not execute (and as such I can't get the log-in bar to show up again because it's collapsed by default).
  EDIT: This problem is now solved, the JS didn't execute because there were errors in it. It referenced a button that only shows up when you are not logged in, obviously that broke the code when you <em>are</em> logged in :)</p>
  
  <p>Here's my code:</p>
  
  <p>_LogOn.cshtml:</p>
  
  <pre><code>@model project_blok_c_groep_13_webshop.Models.LogOnModel
  
  @{
      if (Request.IsAuthenticated)
      {
      &lt;div class="minicolumn"&gt;
          U bent ingelogd als: @User.Identity.Name
      &lt;/div&gt;
      &lt;div class="minicolumn miniborder"&gt;
          Click here to log out!
      &lt;/div&gt;
      }
      else
      {
      &lt;div class="minicolumn"&gt;
          @using (Html.BeginForm("_LogOn", "Account", new { ReturnUrl = Request.QueryString["ReturlUrl"] }))
          {
              &lt;span style="color:red"&gt;@Html.ValidationSummary(true, "Incorrecte gebruikersnaam en/of wachtwoord.")&lt;/span&gt;
              &lt;fieldset&gt;
                  Gebruikersnaam:
                  @Html.TextBoxFor(model =&gt; model.Username)
                  @Html.ValidationMessageFor(model =&gt; model.Username)
  
                  Wachtwoord:
                  @Html.PasswordFor(model =&gt; model.Password)
                  @Html.ValidationMessageFor(model =&gt; model.Password)
  
                  &lt;input type="submit" value="Log-in" /&gt;
              &lt;/fieldset&gt;
          }
      &lt;/div&gt;
      }
  }
  </code></pre>
  
  <p>_Layout.cshtml:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
   &lt;!-- saved from url=(0014)about:internet --&gt;
   &lt;head&gt;
    &lt;title&gt;Webshop&lt;/title&gt;
    &lt;link href="@Url.Content("~/Content/Site.css")" type="text/css" rel="stylesheet" /&gt;
    &lt;link href='http://fonts.googleapis.com/css?family=Merriweather+Sans' rel='stylesheet' type='text/css' /&gt;
  
    &lt;script type="text/javascript"&gt;
        &lt;!--lots of javascript omitted here--&gt;
    &lt;/script&gt;
  
   &lt;/head&gt;
  
   &lt;body onload="loadPage();"&gt;
    &lt;div class="maindiv"&gt;
     &lt;div class="topbar"&gt;
     &lt;/div&gt;
     &lt;div id="minibar"&gt;
      @Html.ActionLink("Log uit", "LogOff","Account")
      &lt;a href="/"&gt;Home&lt;/a&gt;
      &lt;a href="#"&gt;Catalogus&lt;/a&gt;
      &lt;a href="#"&gt;Winkelwagen&lt;/a&gt;
      &lt;a href="#"&gt;Contact&lt;/a&gt;
      &lt;a onclick="displayBar();"&gt;Account&lt;/a&gt;
      &lt;div id="visbar"&gt;
          @Html.Action("_LogOn", "Account")
      &lt;/div&gt;
     &lt;/div&gt;
     &lt;div class="content"&gt;
       &lt;div class="sidebar"&gt;
          @Html.Action("_Menu", "Home")
       &lt;/div&gt;
  
  
       &lt;div class="center"&gt;
        &lt;noscript&gt;
         &lt;p&gt;
          For full functionality of this site it is necessary to enable JavaScript.
          Here are the &lt;a href="http://www.enable-javascript.com/" target="_blank"&gt;
          instructions how to enable JavaScript in your web browser.&lt;/a&gt;
         &lt;/p&gt;
        &lt;/noscript&gt;
  
        @RenderBody()
  
       &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="footer"&gt;
  
      &lt;/div&gt;
     &lt;/div&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And lastly, AccountController:</p>
  
  <pre><code>    public class AccountController : Controller
      {
          private AuthDBController authDBController = new AuthDBController();
  
          public ActionResult LogOff()
          {
              FormsAuthentication.SignOut();
  
              return RedirectToAction("Index", "Home");
          }    
  
          /*
           * Voor de expandable bar.
           */
          public PartialViewResult _LogOn()
          {
              return PartialView();
          }
  
          [HttpPost]
          public ActionResult _LogOn(LogOnModel logOnModel, string returnUrl)
          {
              if (ModelState.IsValid)
              {
                  bool auth = authDBController.isAuthorized(logOnModel.Username, logOnModel.Password);
  
                  if (auth)
                  {
                      FormsAuthentication.SetAuthCookie(logOnModel.Username, false);
                      return Redirect(returnUrl ?? Url.Action("Index", "Home"));
                  }
                  else
                  {
                      ModelState.AddModelError("loginfout", "Incorrecte gebruikersnaam en/of wachtwoord.");
                  }
              }
              return PartialView(logOnModel);
          }
  
      }
  </code></pre>
  
  <p>These problems have me stumped, I've looked through a lot of other similar questions, but none had the answer. Any help would be greatly appreciated. I guess I'm not understanding how partial views, redirects and stuff works.</p>