# PageData value returning null inside partial view

Bassie
1#
Bassie Published in 2017-09-14 07:32:46Z
 I have this code in _ViewStart.cshtml: @{ Layout = "~/Views/Shared/_Layout.cshtml"; var cultureRoute = ViewContext.RouteData.Values["lang"].ToString(); PageData["culture"] = new System.Globalization.CultureInfo(cultureRoute); }  And not having any problems retrieving culture in my views, including the partial _Layout view with this at the top of the page: @{ var culture = @PageData["culture"]; }  However, after creating a new partial view, _Navbar, the exact same piece of code assigns culture to null when calling it from insde _Layout with @Html.Partial("_Navbar")  Why is that PageData variable no longer available?
Michael Domashchenko
2#
Michael Domashchenko Reply to 2017-09-26 12:36:12Z
 The PageData is part of the PageContext. When Razor renders a view it automatically combines it with the _ViewStart page and a Layout page. While it does this, it passes PageContext around so the same PageData is available to all 3 views. Unfortunately, when it gets to partial views, the Html.Partial/RenderPartial both work using HtmlHelper which only passes a model (which can be replaced by explicitly providing a second argument) and ViewData which is part of ViewContext. That's why partial views never see PageData from the view that renders them. If you want to make the culture available in partial views, you can put it into ViewData instead. The only quirk here is that while it's available as a property inside views, layouts and partial pages, in the _ViewStart page you have to access it through the ViewContext property: @{ Layout = "~/Views/Shared/_Layout.cshtml"; var cultureRoute = ViewContext.RouteData.Values["lang"].ToString(); ViewContext.ViewData["culture"] = new System.Globalization.CultureInfo(cultureRoute); }  Then just use ViewData instead of PageData everywhere else: @{ var culture = ViewData["culture"]; }  This blog post has some more details on how Razor uses PageContext while rendering pages.
 You need to login account before you can post.
Processed in 0.317222 second(s) , Gzip On .