I try to understand how works sections in Razor views. I have 2 layout files: _Layout.cshtml and _LayoutChild.cshtml which use the first layout inside. I try to declare sections in _Layout.cshtml and render it in the same file. Markup not appears but when I declare section in _LayoutChild.cshtml everything works. See example below:
_Layout.cshtml:
<!DOCTYPE html> <html> <head> @RenderSection("A", false) </head> <body> @section A{ <script> alert("I'm in Layout!"); </script> } </body> </html> _LayoutChild.cshtml
@{ Layout = "_Layout"; } @section A{ <script> alert("I'm in LayoutChild!"); </script> } I understand that declaring section in the same file looks strange but I would like to know why it don't work?
@RenderSection()but@Html.Partial(). I think this will explain abit better. weblogs.asp.net/scottgu/… while old it is still relevant@RenderBody()is expected in the _layout.cshtml. then once you are rendering for exampleIndex(the section would be inside the actualindexvs the layout).