4

I have a _Layout file that contains this. The _HeaderModel class gets initialized correctly.

<partial name="_Header" model='new _HeaderModel((int?)ViewData["ActiveIndex"])' />

_Header contains

@page
@model Project.Pages.Shared._HeaderModel
@Model.ActiveIndex

I get this error:

'Model' threw an exception of type 'System.NullReferenceException'

Model isn't even a null reference; trying to access the property throws this exception. I really can't do it any more basic. What am I doing wrong?

Edit: This has nothing to do with the generic NullReferenceException thread. I know what a NullReferenceException is (duh) but why would it be happening here? This is a very specific case that regards ASP.NET Core 2.

11
  • Is it ASP.NET Core, right? And are you using controller + view? Commented Mar 4, 2019 at 15:14
  • ASP.NET Core 2.2, using plain Razor page. I know what a NullReferenceException is (duh) but why would it happen here? Commented Mar 4, 2019 at 15:15
  • Have you tried to specify full name in partial to page like /Page/Shared/_Header? Commented Mar 4, 2019 at 15:20
  • That doesn't change anything, except that I must then add .cshtml at the end Commented Mar 4, 2019 at 15:26
  • 8
    Oh, found the answer here!! Removing @page at the top of the partial view fixes it stackoverflow.com/a/52014029/3960200 Commented Mar 4, 2019 at 16:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.