From 35f1514ca8346e0bc275439ff84267f0654d3475 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 2 Dec 2021 12:55:40 -0800 Subject: [PATCH 1/5] Adding an warning message. --- aspnetcore/mvc/models/model-binding.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index f47b585daa..46f64eddb3 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -966,6 +966,8 @@ For targets that are collections of simple types, model binding looks for matche ``` [a]=1050&[b]=2000&index=a&index=b ``` + > [!WARNING] + > Since the model binding looks for the keyword `index` from a Form or Query string is strongly recommended to not use it as your action's parameter name to avoid unexpected behaviors. * The following format is supported only in form data: From 3120134835b3c6a1b4620331d4fd1af23831c58c Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 2 Dec 2021 14:12:14 -0800 Subject: [PATCH 2/5] Update aspnetcore/mvc/models/model-binding.md Co-authored-by: Pranav K --- aspnetcore/mvc/models/model-binding.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index 46f64eddb3..20ae51744a 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -967,7 +967,10 @@ For targets that are collections of simple types, model binding looks for matche [a]=1050&[b]=2000&index=a&index=b ``` > [!WARNING] - > Since the model binding looks for the keyword `index` from a Form or Query string is strongly recommended to not use it as your action's parameter name to avoid unexpected behaviors. + > 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: + + ```csharp + public IActionResult Post(string index, List products) * The following format is supported only in form data: From 86fed85d8b11e542770b09466ae74d0d681f970c Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Dec 2021 12:52:31 -1000 Subject: [PATCH 3/5] Update model-binding.md --- aspnetcore/mvc/models/model-binding.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index 20ae51744a..d7448bf0b7 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -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 ``` + * 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 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 products) + ``` * The following format is supported only in form data: From 0f93c1119391af5cbd7b1799a445624e430d0bab Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Dec 2021 14:45:26 -1000 Subject: [PATCH 4/5] Update aspnetcore/mvc/models/model-binding.md --- aspnetcore/mvc/models/model-binding.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index d7448bf0b7..b265b1c389 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -342,7 +342,10 @@ For targets that are collections of simple types, model binding looks for matche [a]=1050&[b]=2000&index=a&index=b ``` - + 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 products) * The following format is supported only in form data: ``` From 555b3bac81a6d7160467d4ca4b133e12e9c3df1c Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 2 Dec 2021 14:47:35 -1000 Subject: [PATCH 5/5] Update model-binding.md --- aspnetcore/mvc/models/model-binding.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index b265b1c389..d321dde2c6 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -346,7 +346,11 @@ For targets that are collections of simple types, model binding looks for matche ```csharp public IActionResult Post(string index, List products) -* The following format is supported only in form data: + ``` + 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 products) + ``` ``` selectedCourses[]=1050&selectedCourses[]=2000