ASP.NET Core Razor 页面 - 不绑定 POST 请求

Ctr*_*eat 3 c# asp.net-core razor-pages

我的 Login.cshtml.cs 代码文件中有以下内容:

登录.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我在返回行上有一个断点,OnPost并且我从页面中单击了“登录”按钮 - 断点被击中 - 但模型为空。然而,从开发工具来看,模型中的值似乎作为表单数据发送。所以我把[FromBody]改为[FromForm].

但是,当我运行此代码并点击“登录”按钮时,出现以下异常:

InvalidOperationException:无法创建“CarWarehouse.LoginModel”类型的实例。模型绑定复杂类型不得是抽象或值类型,并且必须具有无参数构造函数。

更新

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>
}
Run Code Online (Sandbox Code Playgroud)

注意我正在使用 ID Server 4 并且只是尝试实现与此存储库类似的逻辑 - https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master

Nko*_*osi 5

参考ASP.NET Core 中的模型绑定:[BindProperties] 属性

在 ASP.NET Core 2.1 及更高版本中可用。可以应用于控制器或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();
    }
}
Run Code Online (Sandbox Code Playgroud)