17

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 View(), I always receive a NullReferenceException when even just accessing the Model.

I can't even check if the model is null before executing the rest of the razor page; simply doing a if (Model != null) also throws the same NullReferenceException.

Index.cshtml

@page
@model EncodeModel
@{
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h2>Encode</h2>

<div id="progress">
    @await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())
</div>

EncodeProgress.cshtml

@page
@model FFenc.IEncoderModule

@{
    var module = Model; //this throws the NullReferenceException
}

Startup.cs

    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();
    }

Exception stack trace:

NullReferenceException: Object reference not set to an instance of an object.

AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync() in EncodeProgress.cshtml
var module = Model;

What am I doing wrong? I have attempted multiple fixes and workarounds (using a ViewComponent instead of a view) but nothing works.

Some similar questions that I have found that have not solved my problem:

ASP.NET Core Model null error in the Index view

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 NullReferenceException happened with this code:

    [Route("/encode/progress")]
    public IActionResult GetProgress()
    {
        return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user5860357
  • 173
  • 1
  • 1
  • 4

1 Answers1

38

I think you're mixing Razor Pages with ASP.NET MVC. Try removing the @page directive and your model should be bound to the value passed when you call return View().

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • Thanks, this fixed it. Resharper marks the `Model` variable as undefined without the `@page` directive but I found another question about it: https://stackoverflow.com/questions/33130521/visual-studio-cannot-resolve-model-but-razor-pages-loads . Thanks again! – user5860357 Aug 25 '18 at 04:49
  • Just a heads up, that other question is unrelated. ASP.NET Core (which includes MVC & Razor Pages) is a pretty new development framework whereas ASP.NET MVC (*not* Core) has been around for almost a decade. Razor Pages specifically I think is only about a year old. If you're new to it all it can be a little confusing. There's a lot of overlap, but there's also differences. Core is absolutely the way to go for any new development. I just wanted to let you know so you get off on the right track. – Justin Helgerson Aug 25 '18 at 05:03