Update model-binding.md

pull/24175/head
Rick Anderson 2021-12-02 12:52:31 -10:00 committed by GitHub
parent 3120134835
commit 86fed85d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

View File

@ -342,6 +342,7 @@ For targets that are collections of simple types, model binding looks for matche
[a]=1050&[b]=2000&index=a&index=b
```
<!-- STUB: Make same change here -->
* The following format is supported only in form data:
```
@ -966,11 +967,18 @@ For targets that are collections of simple types, model binding looks for matche
```
[a]=1050&[b]=2000&index=a&index=b
```
> [!WARNING]
> Avoid binding a parameter or a property named `index` or `Index` if it is adjacent to a collection value. Model binding will attempt to use it as the index for the collection which might result in incorrect binding. For example, consider the following action:
Avoid binding a parameter or a property named `index` or `Index` if it is adjacent to a collection value. Model binding attempts to use `index` as the index for the collection which might result in incorrect binding. For example, consider the following action:
```csharp
public IActionResult Post(string index, List<Product> products)
```
In the preceding code, the `index` query string parameter binds to the `index` method parameter and also is used to bind the product collection. Renaming the `index` parameter or using a model binding attribute to configure binding avoids this issue:
```csharp
public IActionResult Post(string productIndex, List<Product> products)
```
* The following format is supported only in form data: