dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

NRE when binding to an array with SupplyParametersFromForm #49744

Closed benjaminsampica closed 1 year ago

benjaminsampica commented 1 year ago

Is there an existing issue for this?

Describe the bug

Attempting to bind arrays (for example, a <select> with multiple) with server-side rendering via [SupplyParameterFromForm] results in a null reference exception.

Expected Behavior

Arrays can be bound correctly.

Steps To Reproduce

  1. Create a new blazor web project - dotnet new blazor
  2. Create an EditForm with an select component bound to an array.
  3. Create another input of some sort inside the form or else EditForm cant even seem to detect the Model.
  4. Add a button with type=submit
  5. Run the app and click the button.

Exceptions (if any)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Components.BindConverter.FormatterDelegateCache.<>c__DisplayClass3_0`1.<MakeArrayFormatter>g__FormatArrayValue|0(T[] value, CultureInfo culture)
   at BlazorApp1.Pages.Index.<BuildRenderTree>b__0_2(RenderTreeBuilder __builder2)
   at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent[TValue](Int32 sequence, RenderFragment`1 fragment, TValue value)
   at Microsoft.AspNetCore.Components.CascadingValue`1.Render(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
   at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView parameters)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32 componentId, ParameterView initialParameters)
   at Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(IComponent component, ParameterView initialParameters)
   at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.RenderEndpointComponent(HttpContext httpContext, Type rootComponentType, ParameterView parameters, Boolean waitForQuiescence)
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore()
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore()
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__9_0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

.NET Version

.NET 8 preview 6

Anything else?

Repro steps in a repo:

https://github.com/benjaminsampica/NREOnArrays/blob/main/BlazorApp1/Pages/Index.razor#L46

javiercn commented 1 year ago

https://github.com/dotnet/aspnetcore/pull/50431