aspnetcore: Blazor @bind:after not working on .NET 7 RTM release
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Cleanly installed VS 17.4, which installed .NET 7: dotnet --list-sdks 7.0.100 [C:\Program Files\dotnet\sdk]
Attempting to run example component:
@page "/"
<input @bind="searchText" @bind:after="PerformSearch" />
@code {
string searchText;
async Task PerformSearch()
{
// ... do something asynchronously with 'searchText' ...
}
}
Project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
dotnet build MSBuild version 17.4.0+18d5aef85 for .NET
Get following error: C:\Users\dtaylor\source\repos\EmptyBlazorApp1\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGe nerators.RazorSourceGenerator\Pages_Index_razor.g.cs(56,132): error CS1503: Argument 3: cannot convert from ‘Microsoft. AspNetCore.Components.EventCallback<string>’ to ‘System.Action<string?>’ [C:\Users\dtaylor\source\repos\EmptyBlazorApp1 \EmptyBlazorApp1.csproj]
Expected Behavior
Expected behaviour is sample included at URL below should work as documented in the blog entry: https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-7/#:~:text=In.NET 7 you can now easily run async,%2F%2F... do something asynchronously with 'searchText'... } }
Steps To Reproduce
Just use: dotnet new blazorserver-empty
Then paste this code into the Pages/Index.razor file:
@page "/"
<input @bind="searchText" @bind:after="PerformSearch" />
@code {
string searchText;
async Task PerformSearch()
{
// ... do something asynchronously with 'searchText' ...
}
}
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
.NET SDK: Version: 7.0.100 Commit: e12b7af219
Runtime Environment: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.100\
Host: Version: 7.0.0 Architecture: x64 Commit: d099f075e4
.NET SDKs installed: 7.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables: Not set
global.json file: Not found
Learn more: https://aka.ms/dotnet/info
Download .NET: https://aka.ms/dotnet/download
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 13
- Comments: 108 (38 by maintainers)
I just read the previous issue https://github.com/dotnet/aspnetcore/issues/44955 and expect my issue is related.
This is one of the most touted features of .NET 7 / Blazor (talked about at .NET Conf) - so if this is broken it needs to be an immediately priority to get a patch out for the RTM release.
Sorry, this is indeed broken in 17.5 / 7.0.200.
The reason for this break is that between 17.4 and 17.5 the razor compiler code based moved from dotnet/razor-compiler to dotnet/razor. This move was done so the razor compiler and IDE / tooling experience could exist in a single repository. That will allow us to move together as a team, make cross cutting changes faster and overall improve the razor experience. It’s an approach we’ve had great success with in roslyn.
Unfortunately the fix for the
@bind:afterscenarios was merged into razor-compiler at the exact time we were doing the migration between the repositories. Our team got quickly consumed by cleaning up the result of merging the repositories and missed that this change was not correctly ported over. As soon as we saw this bug we realized our mistake and are working to correct it.I’m sorry for the disruption this has caused. We’ll be patching 17.5 and 7.0.2xx as soon as we can.
The PR for porting the fix is here
Tracking issue in razor: https://github.com/dotnet/razor/issues/8315
@eyalalonn Unfortunately, this is a mismatch between the code generated by the compiler and the runtime APIs available.
We are working to get the compiler updated in the SDK, but a fix will not be available officially in until 7.0.102. We will however update this thread when we have a nightly build of the SDK available with the fix to unblock folks.
In a previous post I incorrectly pointed out to a different version of the SDK, that seemed to fix the issue, but the issue was that it contained older bits than the RTM version and that’s what made it work.
It also was fixed for me in 17.4 but broken again in 17.5
I just have installed Visual Studio Professional 17.5.0 RTM. I did not have an error before, but now I do.
After installation I have the same error: Both in Visual Studio and with
dotnet builderror CS1503: Argument 3: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<string>' to 'System.Action<string?>'When I add a
global.jsonwith a previous sdk it gives no errorFYI - still not working Blazor-Server 7.0.2 SDK
The same error in 17.5.0 RTM.
Hi folks, a few updates here.
The bits are on the 7.0.1 SDK, so you should be able to build/run/publish from the command line. Check the repo here for a working sample.
There are a couple of things that might get in your way:
[Parameter] public EventCallback<string> ValueChanged { get; set; }is not valid as that is what caused the original issue with additional renders. You need to pass in an action or a task returning function to set/after. This page contains a sample of all the valid combinations https://github.com/javiercn/BindGetSetAfter701/blob/main/BindGetSetAfter701/Client/Pages/Index.razorCould you share when we may expect the update non-prerelease 7.0.x update released?
The problem occurs only with Blazor WebAssembly apps that refence release packages:
If I change release packages to previous rc.2 versions then all goes well:
After installing SDK version 8.0.0-preview.2.23128.3 both the CLI and the VS works as documented.
@danroth27 I also confirm the above bind:get and bind:set compiler error only happen with the input tag, and it works fine with InputText, InputNumber (etc). Although don’t forget to include the @using Microsoft.AspNetCore.Components.Forms if you want to try this. You can maybe tell users to do this as a workaround until the team can address the bind:get and bind:set bugs.
Update for the subscribers in this issue:
@danroth27 replied the following in the issue https://github.com/dotnet/AspNetCore.Docs/issues/27848#issuecomment-1339910661
Which has been added to the docs:
Me too. It compiles and runs fine but errors are showing all the time with latest visual studio
Hi @danroth27 . It looks like the bug is on bind:get and bind:set.
I used the exact same test I originally included in this repo, and you are correct that does compile using dotnet build (ignoring the VS errors).
But if I paste in this example from here: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-7.0
This fails as shown below:
Thus some progress. It looks like the SDK is working for that feature. However the bind:get and bind:set are not working.
.NET 7.0.1 was released. And even after updating that and Visual Studio to the latest Version 17.4.3, I still get the following error in a newly created .NET 7 project:
CS1503 Argument 1: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<string>' to 'System.Func<string, System.Threading.Tasks.Task>'My code is the following:
According to the docs, this should work since I’m using 7.0.1.
I get the same situation even when creating the project using the dotnet CLI and vs code.
Am I missing something?
@bind:setalso do not workI confirm this has not been fixed in .NET 7.0.1 (SDK 7.0.101), and the same error occurs when compiling in Visual Studio or using .NET Build. Some people above have noted their specific code is now compiling when using SDK 7.0.101 from the command line, but I confirm for this example it does not work from the command line in SDK 7.0.101.
@danroth27 This need to be made much more prominent in the documentation, as now a month after RTM one of the key features just does not work and will confuse anyone watching the dotnet conf videos about Blazor in .NET 7.
Just for clarity: The feature works correctly for InputNumber, InputText, just not with the normal html input control, for example:
For some developers they may be switch to use the Input* controls (as a work around) given they no longer need to appear inside an InputForm in .NET 7.
Same here.
After installing .NET 7.0.2, Visual Studio 2022 Preview and creating a
global.jsonI still have this problem in Blazor Server-side.Commands
Edit: I tried .NET 8.0.x as well, but same error. Re-installing .NET 7 RC 2 and changing
global.jsonto the following fixed the issue:Hopefully it’ll be fixed next release…
Yup, we should be ok to remove the notes at this point.
Previously the code below produced error: cannot convert from ‘Microsoft.AspNetCore.Components.EventCallback<int>’ to ‘System.Action<string?>’
I no longer experience the build error in VS Community 17.4.5
Yes, I see Build Succeded and the app runs correctly. But hot reload doesn’t work because for VS there is an error. Every time I have to stop & restart the application. Really annoying.
@iphdav it should be public now.
@guardrex can you adjust the documentation?
Since this is still broken, how would I do this if I were still using .NET 6? There must be some workaround we can use.
Oh right, I can just do
value="@Value" @onchange="OnValueChanged".I’m getting the same error as posted by the OP when using
@bind:get="@Value" @bind:set="@ValueChanged"and I’m using the latest SDK7.0.200as suggested above. Is there any ETA?I’m 100% using the latest build of dotnet (7.0.200-preview.22558.7) and building from the command line; the RC packages is the only way to get this to work.
Do you know if there’s likely to be bugs introduced by developing against the RC packages for now, or is it just performance improvements?
@mbobka
https://learn.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-7.0#:~:text=Components support two-way data binding by defining a pair of parameters%3A
Check that you have used the component correctly. This is an example from the documentation.
In my case, the update helped and everything works fine.
https://github.com/dotnet/installer
Release/7.0.2xx (7.0.x Runtime)
@iphdav thanks for contacting us.
This is a known issue. https://github.com/dotnet/core/blob/main/release-notes/7.0/known-issues.md#70-bind-get-set-after-cant-be-used-in-70-blazor-applications
We had an issue with our dependency flow and one commit did not make it into RTM. A fix will be available in the first patch release.
This is already fixed in
7.0.200-preview.22558.7. If you want to get unblocked, you can use the nightly builds by downloading the SDK fromdotnet/installersor using thedotnet-install.ps1 script. like.\dotnet-install.ps1 -Channel 7.0.2xx -Version 7.0.200-preview.22558.7 -InstallDir ...