C# ASP.NET Razor页面模型绑定不工作
我在剃须刀页面上有以下表格:C# ASP.NET Razor页面模型绑定不工作,c#,asp.net-core,asp.net-core-razor-pages,C#,Asp.net Core,Asp.net Core Razor Pages,我在剃须刀页面上有以下表格: <form method="post"> <table> <tr> <td style="padding-bottom: 10px;" class="login-entry">User Name</td> <td style="padding-bottom: 1
<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
}
公共字符串登录名{get;set;}
公共字符串密码{get;set;}
公共异步任务OnPostAsync()
{
//登录代码在这里
}
问题是,当我调试页面时,输入用户名和密码,然后单击login,在OnPostAsync方法中,LoginName和password属性都为null
有人知道为什么这不起作用吗?或者,因为我对Razor pages还不熟悉,至少有一种方法可以在这些东西不起作用时追踪它们?非常感谢您的帮助。您可以添加此属性
[BindProperty]
,如下所示:
[BindProperty]
public string LoginName { get; set; }
[BindProperty]
public string Password { get; set; }
public async Task<IActionResult> OnPostAsync()
{
// Login code here
}
[BindProperty]
公共字符串登录名{get;set;}
[BindProperty]
公共字符串密码{get;set;}
公共异步任务OnPostAsync()
{
//登录代码在这里
}
结果:
谢谢你的回答!