Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How do I set a local storage item after a form submit in a Blazor component?

$
0
0

I'm using the .NET8 Blazor web app template, with the Identity stuff included, and have added the Blazored.LocalStorage Nuget package.

I would like to set a local storage item when the user logs in, so added a single line to the Login.razor method that is called when the form is submitted (irrelevant code removed for clarity)...

  public async Task LoginUser() {    SignInResult result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);    if (result.Succeeded) {      await LocalStorage.SetItemAsStringAsync("Greeting", "Hello");      RedirectManager.RedirectTo(ReturnUrl);    }  }

However, when this is run, I get an exception....

InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method

OK, so I added the following at the top of the file...

@rendermode @(new InteractiveServerRenderMode(prerender: false))

However, that gives a different exception...

System.InvalidOperationException: Headers are read-only, response has already started

I'm a bit stuck now. Anyone able to explain how I do this?

Thanks


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>