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

时间:2019-07-31 15:23:44

标签: c# asp.net-core razor-pages

我的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();
    }
}

如果我在OnPost的返回行上有一个断点,并且从页面上单击了Login按钮,则该断点被击中-但是该模型为空。但是,从开发工具看来,模型中的值正在作为表单数据发送。因此,我将[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>
}

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

1 个答案:

答案 0 :(得分:4)

引用Model Binding in ASP.NET Core: [BindProperties] attribute

  

在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();
    }
}