How to declare global variables in Blazor

Anonymous
2024-05-23T14:07:57.82+00:00

How to declare global variables in Blazor , for example I have inputquestions in Index I want to use this in another page called Quiz.

@page "/"
<h1>  Quiz </h1>
Select Input Questions and 
Click on  Quiz  .
<div class="row">
    <p>Questions:</p>
    <input type="range" min="5" max="20" step="5" @bind="@inputquestions"
     @bind:event="oninput" />
</div>
<p>@inputquestions</p>
@code {
    public  int inputquestions=5;
}
Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

Answer accepted by question author
  1. Azizkhon Ishankhonov 1,010 Reputation points
    2024-05-23T14:21:19.71+00:00
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,856 Reputation points Volunteer Moderator
    2024-05-23T17:59:40.6766667+00:00

    you did not define a @rendermode, so the default is static (no javascript events).

    0 comments No comments

  2. Bruce (SqlWork.com) 82,856 Reputation points Volunteer Moderator
    2024-05-23T15:25:46.3266667+00:00

    in addition to cascading state you have:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/state-management?view=aspnetcore-8.0&pivots=server

    note: while using a static would work with a Blazor WASM, it will not work with server pre-render or server Blazor as the static value would be shared between all Blazor instances.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.