-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Question] ModelBinding [Required] is MVC5 BackCompat needed? #2615
Comments
@Bartmax the If you prefer to ensure a particular value appears on the wire, please use |
Then I propose to change the BCL semantics, make |
We can't change the underlying BCL semantics - those semantics are not specific to MVC. You have to use the features of the underlying formatter if you want these semantics. |
ok, I hope we can use in mvc I still think it's is a bad design (from a framework api perspective). |
The other thing you can if you want this behavior is just customize Sample:
Now |
@rynowak beautiful! that's something I'm going to use 100% of the time for sure. thanks! |
@rynowak just for curiosity, why that cannot be the default binding configuration mvc uses? |
But as @rynowak showed, your application can easily change how |
I see your point, and from a technical perspective that's fine but I still found very confusing (specially for new comers) that in the following code
Required does nothing, and in a web api endpoint one should use BOTH, I don't see why we cannot have a extra documentation that says that for MVC ModelBinders when a required attribute is used and the property is not included in the request payload it will trigger a modelstate error. |
Back in february, there were an issue (#1971) about binding value types with
[Required]
attribute that doesn't creates a ModelState error.This was 'fixed' and now we can use
and the ModelBinder will trigger a ModelState error if the parameter is not in the request. Which (to me) looks like the correct behavior.
Unfortunatelly, this isn't backcompat with MVC5 (also pointed out by myself on #1971).
Right now, I think there's work to bring back said compatibility (fa56df9) and today I came across a issue about MVC5 Back Compat in a somehow related scenario when using
[FromBody]
here: #2423 (comment)I saw a
[BindRequired]
attribute mentioned here: #2526 and here: #2493, while I'm not sure if this is designed because of bringing back MVC5 compat with the[Required]
attribute, looks like it.Is there any real reason to keep that Back Compatibility or is just because of back-compat ? While I don't think so, I'm asking because I don't see any value of having a
[Required]
attribute that doesn't createModelState
errors on value types, actually I think it's a bug (not intended behavior) in MVC5.What's the purpose of a
[Required]
attribute if it will always have a value and will never trigger the Required expectation ? What am I missing ?The text was updated successfully, but these errors were encountered: