C
C#2y ago
riazuezra

❔ Double property with [Required] data annotation does not validate

How can I make it so that the req body has to have a DiscountPercentage field?
10 Replies
Anton
Anton2y ago
you've just done it with that required
riazuezra
riazuezraOP2y ago
nope I was able to make requests without the field found the solution, apparently I need to make the field nullable so - public double? DiscountPercentage { get; set; }
riazuezra
riazuezraOP2y ago
Stack Overflow
Required attribute for an integer value
I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this attribute is working only for string properties. When no Id is set, Id has value 0 and the model is...
Anton
Anton2y ago
huh well, making it nullable is kinda ridiculous what are you doing on frontend may I ask?
riazuezra
riazuezraOP2y ago
Sure, this is Web API int, double, etc such datatypes assign default value to the variable that could be why it passes the validation
Anton
Anton2y ago
check out this
Anton
Anton2y ago
Stack Overflow
ASP.NET Core [Require] non-nullable types
Here, the question was posed how to validate non-nullable required types. The provided solution to make the field nullable like the following is not desirable in my case. [Required] public int? D...
Anton
Anton2y ago
this one's only for newtonsoft tho, so probably not what you want it seems like the best you can do with system.text.json is requiring a value for all properties, even if it's got a default value, but I'm not sure.
Anton
Anton2y ago
BindRequiredAttribute Class (Microsoft.AspNetCore.Mvc.ModelBinding)
Indicates that a property is required for model binding. When applied to a property, the model binding system requires a value for that property. When applied to a type, the model binding system requires values for all properties that type defines.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server