0

am getting above error "following sections have been defined but have not been rendered"

whenever I try to add style by razor calling syntax.

@section Styles {
   @Styles.Render("~/Content/plugins/iCheck/iCheckStyles")
}

Am new to MVC5 Razor, so please can any one help ?

nirmal
  • 2,143
  • 1
  • 19
  • 29
  • Does you layout include `@RenderSection("styles", false)`? –  May 13 '15 at 10:45
  • no not at all, should I add it ? – nirmal May 13 '15 at 10:46
  • 1
    Yes. That acts as a placeholder for the content defined the views `@section styles {` - typically, for styles, you would place it in the layout's `` tags –  May 13 '15 at 10:48
  • what this function will do ? can you explain please ? so I can use it next time properly. Thanks @StephenMuecke – nirmal May 13 '15 at 10:50
  • Have a look at [this blog](http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor) and also [this answer](http://stackoverflow.com/questions/13089489/asp-net-mvc-explanation-of-section) –  May 13 '15 at 10:53
  • ok got it but false statement will do in it. – nirmal May 13 '15 at 10:56
  • 1
    If you specify `false` as the second parameter, it means that a view that uses the layout does not need to have `@section styles { ..` defined (its optional). Otherwise you need to use `@if (IsSectionDefined("styles ")) { @section styles {` to check the view has the section (or an exception will be thrown if the view does not have `@section styles { ..`) –  May 13 '15 at 11:03

1 Answers1

1

In order to use @section Styles, you layout being used by the view must include

@RenderSection("styles", false)

@RenderSection acts as a placeholder in the layout to render any content that has been defined in in the section. In your case in means that the css files defined in the iCheckStyles bundle will be rendered at the point in the view where @RenderSection is declared. For css files, this would typically be in the <head> tags immediately before @Scripts.Render("~/bundles/modernizr")

Note the second parameter defines whether the view requires a @section Styles { ... }. If the value is false, then @section Styles is optional, other wise the view must include @section Styles or an exception will be thrown.