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.37k stars 9.99k forks source link

Throw informative exception when Input Controls are not surrounded by EditForm #27804

Closed amdav closed 3 years ago

amdav commented 3 years ago

This is obviously a minor issue, but a NullReferenceException is thrown in the scenario (rather than something more useful), and a new user learning Blazor user would probably have no way to debug this. We need a more useful exception to be thrown (I was in Debug mode as well, and there also was no inner exception).

The scenario is using the Blazor Input controls (InputText, etc) without wrapping them with the EditForm. In this case, when in the default ServerPrerendering mode, the NullReferenceException breaks to this line below in the _Host.cshtml file:

<component type="typeof(App)" render-mode="ServerPrerendered" />

Obviously an exception is being thrown during prerendering (due to the code bug), but I cannot see how a new user learning Blazor would be able to figure out what the bug is. The framework needs to throw a more informative exception here to help new users.

Without PreRendering, the error is clear. If this is a more general issue (where PreRendering causes NullReferenceExceptions to be thrown), we need to provide as helpful diagnostics as possible due to ServerPrerendering being the default.

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

tmfink10 commented 3 years ago

I am a new developer (began 6 months ago) and so Blazor is my first exposure to development. I was playing around with understanding how values can be passed from a child component to a parent when suddenly I encountered this error. I have deduced that the exception is being thrown due to the child component and I believe it is because the <InputSelect> is not wrapped in an <EditForm> in the child component (because it is wrapped in one in the parent). I know that this is not a requirement because I have nearly identical code in another project.

My deduction is based on: 1) <InputText> and <InputSelect>, when present within <EditForm> (in Index) either alone or together, do not cause this exception to be thrown. 2) When my test component <InputString>, which is now comprised of very little other than <InputSelect> with a single option, is added to the <EditForm> the exception is thrown.

Apologies if the code is rather sloppy - I have been monkeying with it for some time trying to make it work.

I have created this repo for this issue.

SteveSandersonMS commented 3 years ago

Ideally we'd like to support the input controls existing outside an editcontext.