Attributes like BindRequired make an action parameter to be required, even the usual model validation attributes can be applied. How can you make all properties from a class be required to be present in the request body?
Asked
Active
Viewed 620 times
0

user33276346
- 1,501
- 1
- 19
- 38
-
You could try to create a [custom model binder](https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-5.0), then using the following code to apply ModelBinding Attribute on Action method: `public IActionResult Index([ModelBinder(BinderType = typeof(CustomModelBinder))]User u) `, check [this article](https://www.c-sharpcorner.com/article/custom-model-binding-in-asp-net-core-mvc/). – Zhi Lv Dec 31 '20 at 06:46
1 Answers
-1
If you have access to change the class: decorate all the properties with[Required]
or [BindRequired]
.
If you don't, you should make a DTO (Data Transfer Object) for that class and apply [Required]
or [BindRequired]
attributes.

Mohammad Barbast
- 1,753
- 3
- 17
- 28
-
I already considered this approach, isn't there any other just working with the action parameters? – user33276346 Dec 31 '20 at 03:03
-
So make a class-level validation class. See https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0 – Mohammad Barbast Dec 31 '20 at 05:48