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;
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,592 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,056 Reputation points
    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.


  2. Bruce (SqlWork.com) 66,056 Reputation points
    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

Your answer

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