title: How to use more than one model in a View with asp-for
body: <p>I have looked at multiple answers including the Partial View documentation <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial</a>, <a href="https://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC" rel="nofollow noreferrer">https://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVC</a>, <a href="https://stackoverflow.com/questions/39377058/posting-multiple-forms-on-mvc-razor-view-with-a-single-view-model">Posting multiple forms on MVC Razor View with a Single View Model</a> but I am unable to get their solutions to work. </p>
  
  <p>I have a .NET Core 2.0 project with individual user authentication. I want my Login page to have the login form, the forgot password form, and the forgot password confirmation. I created <strong>Signin.chstml</strong> </p>
  
  <pre><code>@model LoginViewModel
  
  &lt;div class="m-login__signin"&gt;
      &lt;div class="m-login__head"&gt;
          &lt;h3 class="m-login__title"&gt;
              Sign In To Admin
          &lt;/h3&gt;
      &lt;/div&gt;
      &lt;form class="m-login__form m-form" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post"&gt;
          &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
          &lt;div class="form-group m-form__group form-group"&gt;
              &lt;input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" autocomplete="off" /&gt;
              &lt;span asp-validation-for="Email" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="form-group form-group m-form__group"&gt;
              &lt;input asp-for="Password" class="form-control m-input m-login__form-input--last" placeholder="Password" /&gt;
              &lt;span asp-validation-for="Password" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="row m-login__form-sub"&gt;
              &lt;div class="col m--align-left m-login__form-left"&gt;
                  &lt;label asp-for="RememberMe" class="m-checkbox  m-checkbox--focus"&gt;
                      &lt;input asp-for="RememberMe" /&gt;
                      @Html.DisplayNameFor(m =&gt; m.RememberMe)
                      &lt;span&gt;&lt;/span&gt;
                  &lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="col m--align-right m-login__form-right"&gt;
                  &lt;a href="javascript:;" id="m_login_forget_password" class="m-link"&gt;
                      Forget Password ?
                  &lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="m-login__form-action"&gt;
              &lt;button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primary"&gt;Log in&lt;/button&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and <strong>ForgotPass.cshtml</strong></p>
  
  <pre><code> @model ForgotPasswordViewModel
  
      &lt;div class="m-login__forget-password"&gt;
          &lt;div class="m-login__head"&gt;
              &lt;h3 class="m-login__title"&gt;
                  Forgotten Password ?
              &lt;/h3&gt;
              &lt;div class="m-login__desc"&gt;
                  Enter your email to reset your password:
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;form class="m-login__form m-form" asp-action="ForgotPassword" method="post"&gt;
              &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
              &lt;div class="form-group m-form__group"&gt;
                  &lt;input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" id="m_email" autocomplete="off"&gt;
                  &lt;span asp-validation-for="Email" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="m-login__form-action"&gt;
                  &lt;button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air  m-login__btn m-login__btn--primaryr"&gt;
                      Request
                  &lt;/button&gt;
                  &amp;nbsp;&amp;nbsp;
                  &lt;button id="m_login_forget_password_cancel" class="btn btn-outline m-btn m-btn--pill m-btn--custom m-login__btn"&gt;
                      Cancel
                  &lt;/button&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>under the Shared folder. </p>
  
  <p>I have a combined view model under Models</p>
  
  <pre><code> public class LoginForgotPasswordComboViewModel
      {
          public LoginViewModel LoginViewModel { get; set; }
          public ForgotPasswordViewModel ForgotPasswordViewModel { get; set; }
      }
  </code></pre>
  
  <p>my <strong>Login.cshtml</strong></p>
  
  <pre><code>@using System.Collections.Generic
  @using System.Linq
  @using Microsoft.AspNetCore.Http
  @using Microsoft.AspNetCore.Http.Authentication
  @model LoginForgotPasswordComboViewModel
  @inject SignInManager&lt;ApplicationUser&gt; SignInManager
  
  
  @Html.Partial("../Shared/SignIn.cshtml")
   @Html.Partial("ForgotPass")
  </code></pre>
  
  <p>and the Controller <strong>AccountController.cs</strong></p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; Login(LoginViewModel model, string returnUrl = null)
  {
      ViewData["ReturnUrl"] = returnUrl;               
      if (ModelState.IsValid)
      {                   
          var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
          if (result.Succeeded)
          {
               return RedirectToLocal(returnUrl);
          }
          else
          {
              ModelState.AddModelError(string.Empty, "Invalid login attempt.");                    
              return View(model);
          }
      }
  
      return View(model);
  }         
  </code></pre>
  
  <p>The view renders on the page. I am able to login with the correct username &amp; pw. I can successfully request a password reset. My issues are this</p>
  
  <p>1) If the username/password is not correct, I get the error </p>
  
  <blockquote>
    <p>InvalidOperationException: The model item passed into the ViewDataDictionary is >of type 'TestProject.Models.AccountViewModels.LoginViewModel', but this >ViewDataDictionary instance requires a model item of type 'TestProject.Models.AccountViewModels.LoginForgotPasswordComboViewModel'.</p>
  </blockquote>
  
  <p>I tried adding the following to the controller to return the LoginForgotPasswordComboViewModel</p>
  
  <pre><code>var return_model = new LoginForgotPasswordComboViewModel { LoginViewModel = model };
  return View(return_model);
  </code></pre>
  
  <p>Which resulted in the error </p>
  
  <blockquote>
    <p>InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'TestProject.Models.AccountViewModels.LoginForgotPasswordComboViewModel', but this ViewDataDictionary instance requires a model item of type 'TestProject.Models.AccountViewModels.LoginViewModel'.</p>
  </blockquote>
  
  <p>2) After I request a password reset it redirects me to the ForgotPassword page instead of the Login page - where is this controlled by default?</p>