C
C#14mo ago
Alizer

How do I pass data from my partial view to my main layout in ASP Core?

I have this code in my partial view:
// Partial View Lesson.html
@{
ViewData["LessonTitle"] = "Lesson 1";
}
// Partial View Lesson.html
@{
ViewData["LessonTitle"] = "Lesson 1";
}
Now I want to pass this data to my main layout.
// Main _Layout.cshtml
<html>
<body>
<partial name="Lesson"/>
<h1>@ViewData["LessonTitle"]</h1>
</body>
</html>
// Main _Layout.cshtml
<html>
<body>
<partial name="Lesson"/>
<h1>@ViewData["LessonTitle"]</h1>
</body>
</html>
What I want to do here is to set ViewData["LessonTitle"] in my partial layout and get that data in my _Layout.cshtml but I get the error NullReferenceException: Object reference not set to an instance. specifically on the <h1>@ViewData["LessonTitle"]</h1> line, if I remove that line I don't get any error at all so I believe I can't pass data from partial view to layout via ViewData....? If I can't, how do I pass data from my partial to my main layout?
0 Replies
No replies yetBe the first to reply to this messageJoin