.NET Core MVC中的可选模型属性绑定 [英] Optional Model Properties Binding in .NET Core MVC
问题描述
我创建了一个具有以下属性的 AccountModel
I have created one AccountModel
which has following properties
public class AccountModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[StringLength(10, MinimumLength = 6)]
public string Password { get; set; }
[NotMapped] // Does not effect with your database
[Compare("Password")]
[BindRequired]
public string ConfirmPassword { get; set; }
}
我正在尝试对 SignUp
和 SignIn
使用相同的模型.该模型非常适合注册,但是当我将同一模型用于 SignIn
时,我的模型将变为无效.
I am trying to use same model for SignUp
and SignIn
. This model is perfectly working for signup but when I am using same model for SignIn
my model becomes Invalid.
我知道原因,因为我没有传递确认密码"属性的值.我尝试关注
I know the reason because, I am not passing value for Confirm Password property. I tried following
-
我通过使用?"使
NULLABLE
bu它给了我警告(请参阅随附的屏幕截图)
I made
NULLABLE
by using "?" bu it is giving me warning (See attached screenshot)
我还尝试了 [BindRequired]
注释,该注释也无法正常工作.
I have also tried [BindRequired]
annotation which is also not working.
我正在使用ASP.NET Core 3.1,并尝试使用单一模型进行登录和注册.
I am using ASP.NET Core 3.1 and trying to use single Model for both login and signup.
有什么解决办法吗?我正在学习.NET Core和MVC,因此对此主题的了解较少.
Is there any solution for that? I am learning .NET Core & MVC so having less knowledge about this subject.
请提前获得帮助.