title: ASP.NET Core NullReferenceException when just accessing model
body: <p>I am having trouble with attempting to create a view with a strongly typed model. No matter what I pass in as the model to a <code>View()</code>, I always receive a <code>NullReferenceException</code> when even just accessing the <code>Model</code>.</p>
  
  <p>I can't even check if the model is null before executing the rest of the razor page; simply doing a <code>if (Model != null)</code> also throws the same <code>NullReferenceException</code>.</p>
  
  <p><strong>Index.cshtml</strong></p>
  
  <pre><code>@page
  @model EncodeModel
  @{
      Layout = "~/Pages/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;Encode&lt;/h2&gt;
  
  &lt;div id="progress"&gt;
      @await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>EncodeProgress.cshtml</strong></p>
  
  <pre><code>@page
  @model FFenc.IEncoderModule
  
  @{
      var module = Model; //this throws the NullReferenceException
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler("/Error");
              app.UseHsts();
          }
  
          app.UseHttpsRedirection();
          app.UseStaticFiles();
          app.UseCookiePolicy();
  
          app.UseMvc();
      }
  </code></pre>
  
  <p>Exception stack trace:</p>
  
  <blockquote>
    <p>NullReferenceException: Object reference not set to an instance of an object.</p>
    
    <p>AspNetCore.Encoder_MVC_EncodeProgress.get_Model()<br>
    AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync() in EncodeProgress.cshtml<br>
    var module = Model;</p>
  </blockquote>
  
  <p>What am I doing wrong? I have attempted multiple fixes and workarounds (using a ViewComponent instead of a view) but nothing works.</p>
  
  <p>Some similar questions that I have found that have not solved my problem:</p>
  
  <p><a href="https://stackoverflow.com/questions/38472025/asp-net-core-model-null-error-in-the-index-view">ASP.NET Core Model null error in the Index view</a></p>
  
  <p>I'm already passing in the model so this answer doesn't change anything about what I'm doing. For example, when I was trying to use a controller as a workaround, the same <code>NullReferenceException</code> happened with this code:</p>
  
  <pre><code>    [Route("/encode/progress")]
      public IActionResult GetProgress()
      {
          return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());
      }
  </code></pre>