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.6k stars 10.06k forks source link

Form Databinding Not working correctly with Nested object - Static SSR form submission #58988

Open htmlsplash opened 2 weeks ago

htmlsplash commented 2 weeks ago

Is there an existing issue for this?

Describe the bug

.net 8, .net 9, Blazor static SSR page

I have a form with 2 fields, both reference the same nested object within the criteria object to access the start/end date fiels.

<InputDate class="form-control" @bind-Value="CriteriaFromUserInput.Period.Start" /> <InputDate class="form-control" @bind-Value="CriteriaFromUserInput.Period.End" />

When both fields are supplied (filled out) the databinding works and both values are bound respectively. If you fill out only 1 of the fields (start or end date) no binding takes place. The start and end will be null (since they are of type DateTime?).

Here's the project that reproduces that problem: https://github.com/htmlsplash/BlazorWebAppTest

Run project, select Form Test 1 from the nav menu. Fill out both fields, and observe date fields are databound correctly. Then, fill out only one of the date fields (make sure the other date field has no date), observe the problem described. Also, you will note that the filled out previously date field is cleared because of broken databinding. NOTE: I observed this issue when going through nested object in the criteria object. If you move start/end fields to criteria object and bind to these there, individually, it works perfectly fine. NOTE: This problem is only happening when you are using static SSR page, it works fine when using interactive server.

Look at the FormDataBindTest1.razor under pages folder to see code.

Expected Behavior

Filling out one of the 2 date fields should still bind to the correct field when form is submitted.

Steps To Reproduce

See description of the bug.

Exceptions (if any)

No response

.NET Version

.net 8 .net 9

Anything else?

No response

javiercn commented 2 weeks ago

@htmlsplash thanks for contacting us.

What's likely happening is that the date value that is unfilled is being sent as "" and failing to parse as a valid date (at which point we don't bind the object).

You can check this is the case if instead of receiving a DateTimeOffset value you receive a string.

htmlsplash commented 2 weeks ago

Thanks for the explanation, the sample is there for you guys to verify. But, regardless, if you say in your documentation that you support DateTime and nullable types, this should work correctly, and the value received on the server, if it is nullable type, should be null for empty string. In addition, it should behave consistently. I said in the post that if you don't supply any values, you receive nulls (that's good), and if you fill out both dates, you get both dates (that's good). This problem is observed if you fill out one of the dates and when you using nested types on static SSR page.