title: InvalidOperationException: Incorrect Content-Type: ASP.NET Core
body: <p>I have two forms, one for logging in and one for registering. These are both on the same view and using the same model. I'm handling the form submissions with a controller.</p>
  
  <p>I'm getting the following error when visiting the login page</p>
  
  <pre><code>InvalidOperationException: Incorrect Content-Type:
  </code></pre>
  
  <p>Full error log: <a href="https://pastebin.com/JjfDtr6q" rel="nofollow noreferrer">https://pastebin.com/JjfDtr6q</a></p>
  
  <p>I've looked online and cannot find anything related directly to my problem and am not experienced enough in C#/.NET Core to understand what is causing the problem.</p>
  
  <p>My View</p>
  
  <pre><code>@model PastPapers.Models.LoginModel
  
  @{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;Login&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;!-- Register form inside modal --&gt;
      &lt;div id="registerModal" class=""&gt;
          &lt;div class=""&gt;
              &lt;header class=""&gt;
                  &lt;span onclick="document.getElementById('registerModal').style.display = 'none'" class=""&gt;&amp;times;&lt;/span&gt;
                  &lt;h2&gt;Register&lt;/h2&gt;
              &lt;/header&gt;
              &lt;div class=""&gt;
                  &lt;form asp-controller="Login" asp-action="AttemptRegister" method="post"&gt;
                      &lt;input asp-for="newUsername" type="text" placeholder="Username" /&gt;
                      &lt;input asp-for="newEmail" type="email" placeholder="Email" /&gt;
                      &lt;input asp-for="newPassword" type="password" placeholder="Password" /&gt;
                      &lt;input type="submit" value="Register" /&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;!-- Login form --&gt;
      &lt;form asp-controller="Login" asp-action="AttemptLogin" method="post"&gt;
          &lt;input asp-for="username" type="text" placeholder="Username" /&gt;
          &lt;input asp-for="password" type="password" placeholder="Password" /&gt;
          &lt;input type="submit" value="Login" /&gt;
      &lt;/form&gt;
  
      &lt;button onclick="document.getElementById('registerModal').style.display='block'" class=""&gt;Register&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My Controller</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using PastPapers.Models;
  
  namespace PastPapers.Controllers
  {
      public class LoginController : Controller
      {
          public IActionResult Index(LoginModel loginModel = null)
          {
              if (loginModel == null)
              {
                  return View();
              } else
              {
                  return View(loginModel);
              }            
          }
  
          [HttpPost]
          public IActionResult AttemptLogin(LoginModel loginModel = null)
          {
              if (loginModel != null)
              {
                  loginModel.AttemptLogin();
  
                  if (loginModel.loginSuccess)
                  {
                      return RedirectToAction("Index", "Home", null);
                  } else
                  {
                      return RedirectToAction("Index", loginModel);
                  }
  
              } else
              {
                  return RedirectToAction("Index");
              }
          }
  
  
          [HttpPost]
          public IActionResult AttemptRegister(LoginModel loginModel = null)
          {
              if (loginModel != null)
              {
                  loginModel.AttemptRegister();
  
                  if (loginModel.registerSuccess)
                  {
                      return RedirectToAction("Index");
                  } else
                  {
                      return RedirectToAction("Index", loginModel);
                  }
              } else
              {
                  return RedirectToAction("Index");
              }
          }
      }
  }
  </code></pre>
  
  <p>My model</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using MySql.Data.MySqlClient;
  using Newtonsoft.Json;
  using PastPapers.Helpers;
  using Microsoft.AspNetCore.Http;
  
  namespace PastPapers.Models
  {
      public class LoginModel : HttpContextAccessor
      {
          public string username { get; set; }
          public string password { get; set; }
          public bool loginSuccess { get; set; }
          public string loginErrorMessage { get; set; }
  
          public string newUsername { get; set; }
          public string newPassword { get; set; }
          public string newEmail { get; set; }
          public bool registerSuccess { get; set; }
          public string registerErrorMessage { get; set; }
  
          [JsonIgnore]
          public AppDb Db { get; set; }
  
          public LoginModel(AppDb db = null)
          {
              Db = db;
              loginSuccess = false;
              registerSuccess = false;
          }
  
          public LoginModel()
          {
  
          }
  
          public void AttemptRegister()
          {
              try
              {
                  Db.Connection.Open();
  
                  string sql = "INSERT INTO users (id, username, password, email) VALUES (DEFAULT, @username, @password, @email)";
                  MySqlCommand cmd = new MySqlCommand(sql, Db.Connection);
                  cmd.Prepare();
                  cmd.Parameters.AddWithValue("@username", newUsername);
                  cmd.Parameters.AddWithValue("@password", SecurePasswordHasher.Hash(newPassword));
                  cmd.Parameters.AddWithValue("@email", newEmail);
  
                  if (cmd.ExecuteNonQuery() == 0)
                  {
                      System.Diagnostics.Debug.WriteLine("Account failed to create!");
                      registerSuccess = false;
                  } else
                  {
                      registerSuccess = true;
                  }
  
              } catch (Exception ex)
              {
                  registerErrorMessage = "Error connecting to database";
                  System.Diagnostics.Debug.WriteLine(ex.Message);
              }
          }
  
          public void AttemptLogin()
          {
              try
              {
                  Db.Connection.Open();
  
                  string sql = "SELECT password FROM users WHERE username=@username";
                  MySqlCommand cmd = new MySqlCommand(sql, Db.Connection);
                  cmd.Prepare();
                  cmd.Parameters.AddWithValue("@username", username);
                  MySqlDataReader reader = cmd.ExecuteReader();
  
                  if (reader.Read())
                  {
                      string dbPassword = reader.GetString(0);
  
                      if (SecurePasswordHasher.Verify(password, dbPassword))
                      {
                          loginSuccess = true;
                          HttpContext.Session.SetString("username", username);
  
                      } else
                      {
                          loginSuccess = false;
                          loginErrorMessage = "Incorrect password";
                      }
                  } else
                  {
                      loginErrorMessage = "Unknown username";
                  }
  
              } catch (Exception ex)
              {
                  loginErrorMessage = "Error connecting to database";
                  System.Diagnostics.Debug.WriteLine(ex.Message);
              }
  
          }
      }
  }
  </code></pre>