ASP.NET MVC 3 : RenderSection 'Required’ Parameter is Misleading


The other night at a user group meeting, I was discussing ASP.NET MVC 3 with a group of people.  During the discussion of Master Pages vs. Layout Pages, I talked about Sections and how to use RenderSection() to add content to your layout pages.

RenderSection has two overloads:

RenderSection(string sectionName)

RenderSection(string sectionName, bool required)

If you read the comments on the second overload, you’ll read that the required field determines if the section must be defined in any pages that use the layout page.  Additionally, it notes that by not providing the required parameter (the first overload), it will default to False.

I went to show this to the group, and I was presented with an exception stating that the Section must be defined in my View.  Odd.  It was supposed to be optional.

Today, I reflected the MVC code and determined the comment is incorrect.  By not provided the required parameter, it is set to TRUE.

As I understand it, the behavior is correct and this seems to just be a mix up in the Intellisense code.  You should always assume the Section is required, unless you explicitly tell the Layout page it is not required.

comments powered by Disqus