ASP.NET Core Razor页面-POST请求未绑定 [英] ASP.NET Core Razor pages - not binding on POST Request

查看:22
本文介绍了ASP.NET Core Razor页面-POST请求未绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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 Server4,只是尝试实现与此Repo类似的逻辑-https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/tree/master

推荐答案

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

这篇关于ASP.NET Core Razor页面-POST请求未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆