Passing model from Index to _Layout to _Header in ASP.NET Core causes NullReferenceException on get

preview_player
Показать описание

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Passing model from Index to _Layout to _Header in ASP.NET Core causes NullReferenceException on get_Model()

Index
_Layout
_Header
_Header
get_Model()
@page
@{
Layout = "_Layout";
ViewData["_Header"] = new DemoForStackOverflow.Pages.Shared.Header() {IsArriving = true};
}
h2 some content /h2

@page
@{
Layout = "_Layout";
ViewData["_Header"] = new DemoForStackOverflow.Pages.Shared.Header() {IsArriving = true};
}
h2 some content /h2

!DOCTYPE html
html
head
title Demo for Stack Overflow /title
/head
body
div id="header"
@await Html.PartialAsync("_Header", ViewData["_Header"])
/div
h1 Appears on every page below header and above body /h1
div id="body"
@RenderBody()
/div
/body
/html

!DOCTYPE html
html
head
title Demo for Stack Overflow /title
/head
body
div id="header"
@await Html.PartialAsync("_Header", ViewData["_Header"])
/div
h1 Appears on every page below header and above body /h1
div id="body"
@RenderBody()
/div
/body
/html

@page
@model DemoForStackOverflow.Pages.Shared.Header

@if (Model.IsArriving)
{
p Hello /p
}
else
{
p Goodbye /p
}

@page
@model DemoForStackOverflow.Pages.Shared.Header

@if (Model.IsArriving)
{
p Hello /p
}
else
{
p Goodbye /p
}

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DemoForStackOverflow.Pages.Shared;

public class Header : PageModel
{
public bool IsArriving { get; set; }
}

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DemoForStackOverflow.Pages.Shared;

public class Header : PageModel
{
public bool IsArriving { get; set; }
}

Here is the full exception message and stack trace:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]

An unhandled exception has occurred while executing the request.

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

at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.RenderPartialCoreAsync(String partialViewName, Object model, ViewDataDictionary viewData, TextWriter writer)
at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.PartialAsync(String partialViewName, Object model, ViewDataDictionary viewData)
at MicrosoSource of the question:

Question and source license information:
Рекомендации по теме
visit shbcf.ru