How to set page title from partial page in MVC


I am trying to get the page title from a partial page, but I am not doing this in MVC. Do you have any ideas?

in child.cshtml:

@{ViewBag.Title="this is child"}  > this is not working in a child

I tried to get ViewData info like:


in viewpage.cshtml

ViewBag.Title = ViewData["pagetitle"];

in child.cshtml:

ViewData["pagetitle"] = "this is child";
+4
source share
2 answers

Unfortunately, you cannot do this in a partial view.

The reason for this is that by the time your partial view is being processed by the view parser, the main layout (which contains the tags <title></title>) has already been written to the application response stream, ready for blushing in the browser.

, , , , .

, ( - , ), Javascript.

:

<script type="text/javascript">
document.title = '@ViewBag.Title';
</script>
+6

@{ ViewBag.Title = "..."; } , . PartalView.

javascript, :

document.title = "new title";
+4

Source: https://habr.com/ru/post/1614437/


All Articles