Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET核心剃须刀页面-在POST请求时不绑定_C#_Asp.net Core_Razor Pages - Fatal编程技术网

C# ASP.NET核心剃须刀页面-在POST请求时不绑定

C# ASP.NET核心剃须刀页面-在POST请求时不绑定,c#,asp.net-core,razor-pages,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 {

我的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
的返回行上有一个断点,并且我在页面中点击了登录按钮-断点被点击-但是模型为空。然而,从开发工具来看,模型中的值似乎是作为表单数据发送的。因此,我将
[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>
}
@page
@模型MyProj.Pages.LoginModel
@{
ViewData[“标题”]=“需要同意”;
布局=“_布局”;
}
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@if(Model.EnableLocalLogin)
{
本地登录
登录
取消
}
注意,我使用的是ID Server 4,只是尝试实现与此repo-Reference类似的逻辑

在ASP.NET Core 2.1及更高版本中提供。可应用于控制器或
PageModel
类,以告知模型绑定到该类的所有公共属性:

参考文献

在ASP.NET Core 2.1及更高版本中提供。可应用于控制器或
PageModel
类,以告知模型绑定到该类的所有公共属性:


@Ctrl\u Alt\u挫败我建议你花点时间经历@Ctrl\u Alt\u挫败我建议你花点时间经历一下
[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();
    }
}