From 6f793c97f4e2bd8efd9fd63d2d6140e65a64b35f Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 31 Mar 2022 11:12:06 -1000 Subject: [PATCH] param binding with prefix /2 (#25452) * param binding with prefix /2 * param binding with prefix /2 * Update aspnetcore/mvc/models/model-binding.md Co-authored-by: Wade Pickett * Update aspnetcore/mvc/models/model-binding.md Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> Co-authored-by: Wade Pickett Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com> --- aspnetcore/mvc/models/model-binding.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/aspnetcore/mvc/models/model-binding.md b/aspnetcore/mvc/models/model-binding.md index 84ec83117f..b2375975a6 100644 --- a/aspnetcore/mvc/models/model-binding.md +++ b/aspnetcore/mvc/models/model-binding.md @@ -193,9 +193,12 @@ The simple types that the model binder can convert source strings into include t ## Complex types -A complex type must have a public default constructor and public writable properties to bind. When model binding occurs, the class is instantiated using the public default constructor. +A complex type must have a public default constructor and public writable properties to bind. When model binding occurs, the class is instantiated using the public default constructor. -For each property of the complex type, model binding looks through the sources for the name pattern *prefix.property_name*. If nothing is found, it looks for just *property_name* without the prefix. +For each property of the complex type, [model binding looks through the sources for the name pattern](https://github.com/dotnet/aspnetcore/blob/v6.0.3/src/Mvc/Mvc.Core/src/ModelBinding/ParameterBinder.cs#L157-L172) *prefix.property_name*. If nothing is found, it looks for just *property_name* without the prefix. The decision to use the query isn't made per property. For example, with a query containing `?Instructor.Id=100&Name=foo`, bound to method `OnGet(Instructor instructor)`, the resulting object of type `Instructor` contains: + +* `Id` set to `100`. +* `Name` set to `null`. Model binding expects `Instructor.Name` because `Instructor.Id` was used in the preceding query parameter. For binding to a parameter, the prefix is the parameter name. For binding to a `PageModel` public property, the prefix is the public property name. Some attributes have a `Prefix` property that lets you override the default usage of parameter or property name. @@ -246,9 +249,9 @@ Model binding starts by looking through the sources for the key `Instructor.ID`. Several built-in attributes are available for controlling model binding of complex types: -* `[Bind]` -* `[BindRequired]` -* `[BindNever]` +* [`[Bind]`](xref:Microsoft.AspNetCore.Mvc.BindAttribute) +* [`[BindRequired]`](xref:Microsoft.AspNetCore.Mvc.ModelBinding.BindRequiredAttribute) +* [`[BindNever]`](xref:Microsoft.AspNetCore.Mvc.ModelBinding.BindNeverAttribute) > [!WARNING] > These attributes affect model binding when posted form data is the source of values. They do ***not*** affect input formatters, which process posted JSON and XML request bodies. Input formatters are explained [later in this article](#input-formatters).