18

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

1 Answer 1

41

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().

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this fixed it. Resharper marks the Model variable as undefined without the @page directive but I found another question about it: stackoverflow.com/questions/33130521/… . Thanks again!
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.
I've seen this like 3 times since 2018, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.