dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
491 stars 190 forks source link

Data Annotation Required is reporting errors on nonannotated items #7257

Open vsfeedback opened 2 years ago

vsfeedback commented 2 years ago

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work] In .NET 6.0, C# When I'm using data annotations on a view model, some of the items are required but other items should be optional. Yet, all items are being required. ModelState is showing errors of required on items that are not required.

From the page - showing the error messages

The Title to use field is required. The Middle Name field is required. The Suffix field is required. Corporate Employee ID Admin Title to use The Title to use field is required. First Name David Middle Name The Middle Name field is required. Last Name * Potter Suffix The Suffix field is required.

Version of Visual Studio: Microsoft Visual Studio Professional 2022 Version 17.1.1 VisualStudio.17.Release/17.1.1+32228.430 Microsoft .NET Framework Version 4.8.04084

Installed Version: Professional

Visual C++ 2022 00476-80000-00000-AA970 Microsoft Visual C++ 2022

.NET Core Debugging with WSL 1.0 .NET Core Debugging with WSL

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 2019 17.1.358.51495 ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 17.1.358.51495 For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 17.1.358.51495 Azure App Service Tools v3.0.0

Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 17.1.358.51495 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 4.1.0-5.22109.6+0c82c4114a4e4b8b723b915eee3b13261db6717f C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter 17.0.21344.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Fabric.DiagnosticEvents 1.0 Fabric Diagnostic Events

Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio 17.0 Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics

Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.161+abc97ecc7d. RR Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.2 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

NuGet Package Manager 6.1.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Office Developer Tools for Visual Studio 17.0.32009.00 Microsoft Office Developer Tools for Visual Studio

Python - Django support 17.0.21344.1 Provides templates and integration for the Django web framework.

Python - Profiling support 17.0.21344.1 Profiling support for Python projects.

Python with Pylance 17.0.21344.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Razor (ASP.NET Core) 17.0.0.2206201+62a2c1d6162f828801565a7ca26d9d48b810a05b Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.0.62201.12090 Microsoft SQL Server Data Tools

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 17.0.1229.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 4.1.0-5.22109.6+0c82c4114a4e4b8b723b915eee3b13261db6717f Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 17.1.0-beta.21610.4+07b5673e4f2fa7630e78abe37f16b372353a7242 Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio.

Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.

Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager.

Here is the class I'm using:

using System.ComponentModel.DataAnnotations;

namespace Onboarding.Models.ViewModels.Company { public class CompanyAdministratorView { public int ID { get; set; } [StringLength(10)] public string ProviderCode { get; set; } = ""; [StringLength(10)] public string EmployerCode { get; set; } = "";

[Required] [StringLength(60)] [Display(Name = "Corporate Employee ID *")] public string CompanyEmployeeId { get; set; } = "";

[StringLength(10)] [Display(Name = "Title to use")] public string TitleToUse { get; set; } = "";

[Required] [StringLength(80)] [Display(Name = "First Name *")] public string FirstName { get; set; } = "";

[StringLength(80)] [Display(Name = "Middle Name")] public String MiddleName { get; set; } = "";

[Required] [StringLength(100)] [Display(Name = "Last Name *")] public string LastName { get; set; } = "";

[StringLength(10)] public string Suffix { get; set; } = "";

[Required] [StringLength(100)] [Display(Name = "Email Address *")] [DataType(DataType.EmailAddress)] public string eMail { get; set; } = "";

}

and the page @page @using Onboarding.Pages.Administration.Platform.CompanyAdministrators @model CreateModel

@{ ViewData["Title"] = "Create Administrator"; }

Create

Administrator


This creates the company administrator with the default company employee id of "Admin"

Back to List

@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }


Original Comments

Feedback Bot on 3/16/2022, 05:48 AM:

(private comment, text removed)

David Randolph on 3/22/2022, 07:33 AM:

(private comment, text removed)


Original Solutions

(no solutions)

pranavkm commented 2 years ago

It's very likely a result of https://docs.microsoft.com/aspnet/core/mvc/models/validation#non-nullable-reference-types-and-required-attribute. The feature has an option to opt out, could you try that?

ghost commented 2 years ago

Hi @vsfeedback. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 2 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.