dotnet / dotnet/aspnetcore

TryValidateModel expects all object fields to be required

Open
#54,459 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

TryValidateModel method always validates all class fields as if they were marked with [Required] attribute - if the class was put into Razor Class Library - effectively breaking business logic.

If the class of the object being validated is put into the main project - everything works as expected.

### Expected Behavior

TryValidateModel method should validate objects from class from all projects - according to the attributes put on the class fields.

### Steps To Reproduce

1) Create asp.net core project - let's call it 'main project'
2) Create Razor Class Library project - add 'some model' - with a string property
3) in the main project try to validate the 'same model' using TryValidateModel method - while the string property still being null
4) you will see that the response if 'false' and ModelState shows that the string property is required - which is not - because there is no [Required] attribute.

### Exceptions (if any)

_No response_

### .NET Version

.net 6.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.