-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Invalid dynamic collection binding and validation #2294
Comments
@anfomin Are you literally sending |
@dougbu We should at least be consistent with the MVC 5 behavior. |
@danroth27 yes, in previous message I wrote a shorthand. I have 2 inputs: |
@anfomin could you please upload a small sample of what you're trying to do? Want to avoid testing a slightly different scenario. A GitHub repository is easiest for me but feel free to send a Zip file offline or whatever. |
@dougbu ok, I'll prepare sample tomorrow. |
@dougbu sample is ready: https://github.com/anfomin/MvcCollectionBindingSample |
thanks @anfomin. I am able to reproduce the tightly-related issues you reported. both appear to be regressions compared to MVC 5. one workaround for the for (var index = 0; index < Model.Collection.Count; index++)
{
@Html.LabelFor(model => model.Collection[index])
<div>
@Html.EditorFor(model => model.Collection[index])
@Html.ValidationMessageFor(model => model.Collection[index])
</div>
} have not found a workaround for the problem binding directly to a collection (except not doing that at all). |
@dougbu I can't use workaround with numeric indexes because collection items can be added or deleted with JS in real app. So resulting bound collection will have gaps in indexes. I'm using custom |
|
- parts 1/3 and 2/3 of #2294 - fill readonly gaps in `CollectionModelBinder` and `MutableObjectModelBinder` - `CollectionModelBinder.CopyToModel()` and `MutableObjectModelBinder.AddToProperty()` methods - add `CopyToModel()` override in `ArrayModelBinder` - avoid NREs in `GetModel()` overrides Test handling of readonly collections - previous tests barely touched this scenario nits: - add missing `[NotNull]` attributes - add missing doc comments - consolidate a few `[Fact]`s into `[Theory]`s - simplify some wrapping; shorten a few lines
- parts 1/3 and 2/3 of #2294 - fill readonly gaps in `CollectionModelBinder` and `MutableObjectModelBinder` - `CollectionModelBinder.CopyToModel()` and `MutableObjectModelBinder.AddToProperty()` methods - add `CopyToModel()` override in `ArrayModelBinder` - avoid NREs in `GetModel()` overrides Test handling of readonly collections - previous tests barely touched this scenario nits: - add missing `[NotNull]` attributes - add missing doc comments - consolidate a few `[Fact]`s into `[Theory]`s - simplify some wrapping; shorten a few lines
- parts 1/3 and 2/3 of #2294 - fill readonly gaps in `CollectionModelBinder` and `MutableObjectModelBinder` - `CollectionModelBinder.CopyToModel()` and `MutableObjectModelBinder.AddToProperty()` methods - add `CopyToModel()` override in `ArrayModelBinder` - avoid NREs in `GetModel()` overrides Test handling of readonly collections - previous tests barely touched this scenario nits: - add missing `[NotNull]` attributes - add missing doc comments - consolidate a few `[Fact]`s into `[Theory]`s - simplify some wrapping; shorten a few lines
- parts 1/3 and 2/3 of #2294 - fill readonly gaps in `CollectionModelBinder` and `MutableObjectModelBinder` - `CollectionModelBinder.CopyToModel()` and `MutableObjectModelBinder.AddToProperty()` methods - add `CopyToModel()` override in `ArrayModelBinder` - avoid NREs in `GetModel()` overrides Test handling of readonly collections - previous tests barely touched this scenario nits: - add missing `[NotNull]` attributes - add missing doc comments - consolidate a few `[Fact]`s into `[Theory]`s - simplify some wrapping; shorten a few lines
- part 1/2 of #2294 - handle readonly non-`null` collections in relevant binders - `CollectionModelBinder.CopyToModel()` and `MutableObjectModelBinder.AddToProperty()` methods - handle read-only controller properties in `DefaultControllerActionArgumentBinder` - do not copy into arrays e.g. add `CopyToModel()` override in `ArrayModelBinder` - remove ability to set a private controller property - confirm `SetMethod.IsPublic` in `DefaultControllerActionArgumentBinder` - avoid NREs in `GetModel()` overrides Test handling of readonly collections - previous tests barely touched this scenario - also add more tests setting controller properties nits: - add missing `[NotNull]` attributes - add missing doc comments - consolidate a few `[Fact]`s into `[Theory]`s - simplify some wrapping; shorten a few lines - remove dead code in `DefaultControllerActionArgumentBinder` and `ControllerActionArgumentBinderTests`
addressed item (2), the lack of support for copying entries into a collection in 3fd4991 |
@harshgMSFT if you get back to this before I do, I have a branch and a few thoughts to share... |
@dougbu asked to create new issue in discussion #2268. Current
CollectionModelBinder
can't properly bind collections with dynamic indexes. It returns invalid results in 2 cases.1. I've class with collection property:
Binding form data:
If I try to bind
RootObject
thenCollection
property is bound successfully, but ModelState contains unvalidated states forCollection[key1].Name
andCollection[key2].Name
:2. I've the same classes. If I try to bind collection of
AnotherObject
directly with prefix, result collection is still empty:I'm using latest beta5 MVC version.
The text was updated successfully, but these errors were encountered: