【问题标题】:ASP.NET Razor page model binding not workingASP.NET Razor 页面模型绑定不起作用
【发布时间】:2021-03-07 19:34:30
【问题描述】:

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

<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 页面的新手,至少有一种方法可以在它们不起作用时追踪这些东西?任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    你可以添加这个属性[BindProperty],像这样:

        [BindProperty]
        public string LoginName { get; set; }
        [BindProperty]
        public string Password { get; set; }
    
        public async Task<IActionResult> OnPostAsync()
        {
            // Login code here
        }
    

    结果:

    【讨论】:

    • 感谢您的回答!
    猜你喜欢
    • 2022-01-11
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多