title: ASP.NET Core Razor pages - not binding on POST Request
body: <p>I have the following on my Login.cshtml.cs code file:</p>
  
  <p>Login.cshtml.cs</p>
  
  <pre><code>public class LoginModel : PageModel
  {
      public string ReturnUrl { get; set; }
      public bool EnableLocalLogin { get; set; } = true;
      public string Username { get; set; }
      public string Password { get; set; }
  
      private readonly IIdentityServerInteractionService _interaction;
  
      public LoginModel(IIdentityServerInteractionService interaction)
      {
          _interaction = interaction;
      }
  
      public IActionResult OnGet(string returnUrl)
      {            
          ReturnUrl = returnUrl;
          return Page();
      }
  
      public IActionResult OnPost([FromBody]LoginModel model, string button)
      {
          // todo - implement
          return Page();
      }
  }
  </code></pre>
  
  <p>If I have a breakpoint on the return line of <code>OnPost</code> and I hit the Login button from the page - the breakpoint is hit - however the model is null.  From Dev Tools however it seems the values from the model are being sent as form data.  So I changed the <code>[FromBody]</code> to <code>[FromForm]</code>.</p>
  
  <p>However when I run this code and hit the Login button I get the following exception:</p>
  
  <blockquote>
    <p>InvalidOperationException: Could not create an instance of type 'CarWarehouse.LoginModel'. Model bound complex types must not be abstract or value types and must have a parameterless constructor.</p>
  </blockquote>
  
  <p>UPDATE</p>
  
  <p>The Login.cshtml page:</p>
  
  <pre><code>@page
  @model MyProj.Pages.LoginModel
  @{
      ViewData["Title"] = "Consent Required";
      Layout = "_Layout";
  }
  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  
  @if (Model.EnableLocalLogin)
  {
      &lt;div class="col-sm-6"&gt;
          &lt;div class="panel panel-default"&gt;
              &lt;div class="panel-heading"&gt;
                  &lt;h3 class="panel-title"&gt;Local Login&lt;/h3&gt;
              &lt;/div&gt;
              &lt;div class="panel-body"&gt;
  
                  &lt;form asp-route="Login"&gt;
                      &lt;input type="hidden" asp-for="ReturnUrl" /&gt;
  
                      &lt;fieldset&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label asp-for="Username"&gt;&lt;/label&gt;
                              &lt;input class="form-control" placeholder="Username" asp-for="Username" autofocus&gt;
                          &lt;/div&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label asp-for="Password"&gt;&lt;/label&gt;
                              &lt;input type="password" class="form-control" placeholder="Password" asp-for="Password" autocomplete="off"&gt;
                          &lt;/div&gt;
  
                          &lt;div class="form-group"&gt;
                              &lt;button class="btn btn-primary" name="button" value="login"&gt;Login&lt;/button&gt;
                              &lt;button class="btn btn-default" name="button" value="cancel"&gt;Cancel&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/fieldset&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>Note I am using ID Server 4 and just trying too implement similar logic to this repo - <a href="https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master" rel="nofollow noreferrer">https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master</a></p>