title: Both login and registration form on one view MVC3 Razor
body: <p>My task is create both login and registration form on one view!</p>
  
  <p>I have home view. On a home view I have two forms, login and register. They rendered by @Html.RenderPartial(partial view name, model). That forms relates to Account controller (Login and Register actions).</p>
  
  <p>Let me first provide you with some code…</p>
  
  <p><strong>Models</strong>:</p>
  
  <pre><code>// Login model, that contains email and password
  public class Login
  {
      …
  }
  // Registration model that contains minimum user data, e.g. name, surname
  public class Register
  {
      ...
  }
  // Model for home view which contains models for both register and login
  public class Home
  {
      public Login    LoginModel    { get; set; }
      public Register RegisterModel { get; set; }
  }
  </code></pre>
  
  <p><strong>Views</strong>:
  Index.cshtml</p>
  
  <pre><code>@model App.Models.Home.Home
  &lt;!-- Login Box --&gt;
  @{ Html.RenderPartial("_LoginArea", Model.LoginModel); }
  &lt;!-- Register Box --&gt;
  @{ Html.RenderPartial("_RegisterArea", Model.RegisterModel); }
  </code></pre>
  
  <p>_LoginArea.cshtml</p>
  
  <pre><code>@model App.Models.Account.Login
  &lt;div style="float: left; margin-right: 100px;"&gt;
      &lt;h1&gt;@Localization.Account.Login&lt;/h1&gt;
      &lt;!-- Login form --&gt;
      &lt;div id="loginBox"&gt;
      @using (Html.BeginForm("Login", "Account", FormMethod.Post)) 
      {    
          &lt;div class="editor-label"&gt;
          @Html.LabelFor(m =&gt; m.Email):
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
          @Html.TextBoxFor(m =&gt; m.Email)
          @Html.ValidationMessageFor(m =&gt; m.Email)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
          @Html.LabelFor(m =&gt; m.Password):
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
          @Html.PasswordFor(m =&gt; m.Password)
          @Html.ValidationMessageFor(m =&gt; m.Password)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
          @Html.CheckBoxFor(m =&gt; m.RememberMe)
          @Html.LabelFor(m =&gt; m.RememberMe)
          &lt;/div&gt;
          &lt;p&gt;
              &lt;input type="submit" value="@Localization.Account.Login" /&gt;
          &lt;/p&gt;    
      }
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>_RegisterArea.cshtml</p>
  
  <pre><code>@model App.Models.Account.Register
  &lt;div style="vertical-align: bottom;"&gt;
      &lt;h1&gt;@Localization.Account.Register&lt;/h1&gt;
      &lt;div&gt;
      @using (Html.BeginForm("Register", "Account"))
      {
          //same things like in login
      } 
      ...
  </code></pre>
  
  <p><strong>Controllers:</strong></p>
  
  <p>HomeController</p>
  
  <pre><code>//
  // GET: /Home/
  
  public ActionResult Index(Home model)
  {
      //
      // If logedin redirect to profile page
      // Else show home page view
      //
  
      if (Request.IsAuthenticated)
      {
          return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name });
      }
      else
      {
          model.LoginModel = new Login();
          model.RegisterModel = new Register();
          return View(model);
      }
  }
  </code></pre>
  
  <p>Just show you Login action from Account controller</p>
  
  <pre><code>// 
  // POST: /Account/Login
  
  [HttpPost]
  public ActionResult LogIn(Login model)
  {
      if (Request.IsAuthenticated)
      {
          return RedirectToAction("Index", "User", new { id = HttpContext.User.Identity.Name });
      }
      else
      {
          if (ModelState.IsValid)
          {
              if (model.ProcessLogin())
              {
                  return RedirectToAction("Index", "User", new { id = HttpContext.Session["id"] });
              }
           }
       }
  
       //Note: this is problem part
       // If we got this far, something failed, redisplay form
       return View("~/Views/Home/Index.cshtml", model);
  }
  </code></pre>
  
  <p>So everything works fine, <strong>BUT</strong>! When model state is not valid I have following exception</p>
  
  <pre><code>The model item passed into the dictionary is of type 'App.Models.Account.Login', but this dictionary requires a model item of type App.Models.Home.Home'.
  </code></pre>
  
  <p>I can bind that partial views and account actions to Home model, but this is not what I need.
  I planning to use _LoginArea.cshtml view in other views (for example in a page header of ‘User’ view, and that view has another model)
  So, I need action method (e.g. Login) to retrieve Login model, not Home or whatever else. But in this case I’m unable to return model to Home view.
  How can I solve this issue? What is a best proper way?
  Sorry for a lot of code, just want clear things.</p>