[英]ASP.NET Core Razor pages - not binding on POST Request
I have the following on my Login.cshtml.cs code file: 我的Login.cshtml.cs代码文件中包含以下内容:
Login.cshtml.cs Login.cshtml.cs
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();
}
}
If I have a breakpoint on the return line of OnPost
and I hit the Login button from the page - the breakpoint is hit - however the model is null. 如果我在
OnPost
的返回行上有一个断点,并且单击了页面上的“登录”按钮-会遇到断点-但是模型为null。 From Dev Tools however it seems the values from the model are being sent as form data. 但是,从开发工具看来,模型中的值正在作为表单数据发送。 So I changed the
[FromBody]
to [FromForm]
. 所以我将
[FromBody]
更改为[FromForm]
。
However when I run this code and hit the Login button I get the following exception: 但是,当我运行此代码并单击“登录”按钮时,出现以下异常:
InvalidOperationException: Could not create an instance of type 'CarWarehouse.LoginModel'.
InvalidOperationException:无法创建“ CarWarehouse.LoginModel”类型的实例。 Model bound complex types must not be abstract or value types and must have a parameterless constructor.
模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数构造函数。
UPDATE UPDATE
The Login.cshtml page: Login.cshtml页面:
@page
@model MyProj.Pages.LoginModel
@{
ViewData["Title"] = "Consent Required";
Layout = "_Layout";
}
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@if (Model.EnableLocalLogin)
{
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Local Login</h3>
</div>
<div class="panel-body">
<form asp-route="Login">
<input type="hidden" asp-for="ReturnUrl" />
<fieldset>
<div class="form-group">
<label asp-for="Username"></label>
<input class="form-control" placeholder="Username" asp-for="Username" autofocus>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input type="password" class="form-control" placeholder="Password" asp-for="Password" autocomplete="off">
</div>
<div class="form-group">
<button class="btn btn-primary" name="button" value="login">Login</button>
<button class="btn btn-default" name="button" value="cancel">Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
</div>
}
Note I am using ID Server 4 and just trying too implement similar logic to this repo - https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master 注意我正在使用ID Server 4并尝试实现与该存储库类似的逻辑-https: //github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master
Reference Model Binding in ASP.NET Core: [BindProperties] attribute ASP.NET Core中的参考模型绑定:[BindProperties]属性
Available in ASP.NET Core 2.1 and later.
在ASP.NET Core 2.1和更高版本中可用。 Can be applied to a controller or
PageModel
class to tell model binding to target all public properties of the class:可以应用于控制器或
PageModel
类,以告知模型绑定以该类的所有公共属性为目标:
[BindProperties(SupportsGet=true)]
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(string button) {
//access properties here which should be populated from form.
// todo - implement
return Page();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.