.NET Core MVC中的可选模型属性绑定 [英] Optional Model Properties Binding in .NET Core MVC

查看:57
本文介绍了.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

  1. 我通过使用?"使 NULLABLE bu它给了我警告(请参阅随附的屏幕截图)

  1. 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.

请提前获得帮助.

推荐答案

这基于其

可空上下文可以对编译器如何解释引用类型变量进行细粒度控制.任何给定源行的可为空的注释上下文都是启用或禁用的.您可以将C#8.0之前的编译器视为在禁用的可空上下文中编译所有代码:任何引用类型都可以为空.可为空的警告上下文也可以启用或禁用.可为空的警告上下文指定编译器使用其流分析生成的警告.

Nullable contexts enable fine-grained control for how the compiler interprets reference type variables. The nullable annotation context of any given source line is either enabled or disabled. You can think of the pre-C# 8.0 compiler as compiling all your code in a disabled nullable context: any reference type may be null. The nullable warnings context may also be enabled or disabled. The nullable warnings context specifies the warnings generated by the compiler using its flow analysis.

您可以像这样消除此警告:

You can get rid of this warning like this:

#nullable enable
    public string? ConfirmPassword { get; set; }
#nullable disable

作为另一种解决方法,如果您想始终消除项目中的此警告,则可以在 .csproj 文件中添加 Nullable 元素.右键单击您的项目,然后单击编辑项目文件",然后将 Nullable 元素添加到您的 PropertyGroup 部分,如下所示:

As another workaround, if you want to always get rid of this warning in your project, you can add the Nullable element in your .csproj file. Right click on your project and click "Edit Project File", then add the Nullable element to your PropertyGroup section, just like the following:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Nullable>enable</Nullable>
</PropertyGroup>

您只需要将项目重新构建一次,即可查看结果.

You just need to Re-Build your project once to see the result.

这篇关于.NET Core MVC中的可选模型属性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆