title: Blazor NavigationManager NullReference error in code behind
body: <p>Using Blazor in VS19 Professional I'm getting a NullReference exception when I try to use the NavigationManager.NavigateTo function from the code behind .razor.cs file.</p>
  
  <p>I first inject the NaviagtionManager at the top of the .razor.cs file:</p>
  
  <pre><code>[Inject]
  protected NavigationManager NavigationManager { get; set; }
  </code></pre>
  
  <p>Then when the users clicks a submit button, data is saved to an SQL database (this part works fine) and then the page should then redirect to another page, but it fails due to <strong><em>"NavigationManager.get returned null"</em></strong> error.</p>
  
  <p>This is the function saving the data and calling the NavigationManager:</p>
  
  <pre><code>public void Update() {
  
          participants p = new participants {
              ID = Convert.ToInt32(id),
              FirstName = participant[0].FirstName,
              LastName = participant[0].LastName,
              Eating = participant[0].Eating,
              Bowling = participant[0].Bowling,
              EscapeRoom = participant[0].EscapeRoom
          };
  
          this.Db.UpdateParticipant(p);
          NavigationManager.NavigateTo("Bowling/2020");
      }
  </code></pre>
  
  <p>I have been unable to find anything online to help me with this issue.</p>
  
  <p>Does anyone have any idea how to solve this??</p>
  
  <p>UPDATE WITH MORE CODE:</p>
  
  <p>On the .razor page I have an EditForm which on submit calls an Update():</p>
  
  <pre><code>&lt;EditForm Model="@participant" OnValidSubmit="@Update"&gt;
  </code></pre>
  
  <p>On the .razor.cs page, this is the Update() function:</p>
  
  <pre><code>public void Update() {
  
          participants p = new participants {
              ID = Convert.ToInt32(id),
              FirstName = participant[0].FirstName,
              LastName = participant[0].LastName,
              Eating = participant[0].Eating,
              Bowling = participant[0].Bowling,
              EscapeRoom = participant[0].EscapeRoom
          };
  
          this.Db.UpdateParticipant(p);
          NavigationManager.NavigateTo("/Bowling/2020/");
      }
  </code></pre>
  
  <p>The values in the database get updated correctly.</p>
  
  <p>UPDATE:</p>
  
  <p>Found the issue, see response below.</p>