ASP.NET Razor 页面模型绑定不起作用

ASP.NET Razor page model binding not working

我在剃须刀页面上有以下表格:

<form method="post">
    <table>
        <tr>
            <td style="padding-bottom: 10px;" class="login-entry">User Name</td>
            <td style="padding-bottom: 10px;"><input type="text" asp-for="LoginName" class="logins" autocomplete="off" /></td>
        </tr>
        <tr>
            <td style="padding-bottom: 10px;" class="login-entry">Password</td>
            <td style="padding-bottom: 10px;"><input type="password" asp-for="Password" class="logins" /></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align:right;"><input type="submit" value="login" /></td>
        </tr>
    </table>
</form>

然后,在我的 PageModel 中,我有两个属性和 OnPostAsync:

public string LoginName { get; set; }

 public string Password { get; set; }

 public async Task<IActionResult> OnPostAsync()
{
    // Login code here
}

问题是当我调试页面时,输入用户名和密码,然后单击登录,在 OnPostAsync 方法中,属性 LoginName 和 Password 都是空的。

有人知道为什么这不起作用吗?或者,因为我是 Razor 页面的新手,至少有一种方法可以在它们不起作用时追踪这些东西?任何帮助将不胜感激。

您可以添加此属性 [BindProperty],如下所示:

    [BindProperty]
    public string LoginName { get; set; }
    [BindProperty]
    public string Password { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        // Login code here
    }

结果: