繁体   English   中英

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
    }

结果: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM