5

This seems very strange to me, if I do

@RenderSection("scripts", required: false) 

then it works perfectly fine, but if I do

@{ RenderSection("scripts", required: false); } 

then the scripts section will not get rendered and I would get "The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "scripts"." error

Any idea why RenderSection/Script.Render cannot be inside a code block?

Edit: I have tried to put a break point inside the code block and the break point is getting hit when the page loads, and the RenderSection method executes without any exception

1 Answer 1

4

RenderSection does not write anything. Instead this methods returns an HelperResult which implements IHtmlString and can be render to the page by using its WriteTo method.

@{ HelperResult renderSection = RenderSection("scripts", required: false); renderSection.WriteTo(Output); } 

When using @RenderSection it automatically render it to the page

Sign up to request clarification or add additional context in comments.

6 Comments

so it means @ and @{} syntax are actually different in some ways?
@Steve they are very different. @{} is a code block, @ is not
@RenderSection and @(RenderSection) are equivalents, but @{RenderSection} is totally different.
@ErikFunkenbusch mind point out the difference between those two? always thought they would do the same thing, except one is for one line and the other one is for multiple
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.