2

I am successfully using the Nancy Razor engine. I have a _Master.cshtml Layout which defines a razor @section for my javascript and others for my @Html.Partial() and CSS files.

This all works fine.

Edit 1 See here for more useful info including a neat extension method that fixes the problem:

_Master Layout

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
<!DOCTYPE html>
<html lang="en">
    <head>
        //Head stuff
        @RenderSection("CSS", required: false)
    </head>
    <body>
        //Body Stuff
        @RenderBody()
        @RenderSection("Partial", required: false)
        @RenderSection("JS", required: false)
    </body> 
</html>

A Example View

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
    Layout = "_Master.cshtml";
}

// View HTML

@section Partial{
    @Html.Partial("Modal/FindWizard")
}

@section JS{
    <script src="/Scripts/Coffee/example.view.min.js"> </script>
}

You can see from the Example View above that I have included a Partial called FindWizard. I would like this file to also take advantage of the razor sections defined in the _Master Layout.

Something like this:

FindWizard Partial with @sections

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>

<div>
    //.. Wizard HTML
</div>

@section CSS{
    <link href="/Content/css/bootstrap-wizard.css" rel="stylesheet" />
}

@section JS{
    <script src="/Scripts/min/bootstrap-wizard.min.js"></script>
}

I have tried this but I cannot get the razor @sections from the Partial file to render into the final response.

Is this technique possible? Maybe its a plain bad idea as it may lead to duplicate js files being included if a Partial is re-used in the same view?

Thanks

Community
  • 1
  • 1
biofractal
  • 18,963
  • 12
  • 70
  • 116
  • 3
    This is a limitation of Razor (not Nancy's) that you cannot have section inside partial: http://stackoverflow.com/questions/5355427/populate-a-razor-section-from-a-partial – nemesv Mar 12 '13 at 12:07
  • Is that because its a bad idea or is it just an oversight? I know that nancy recently started using a custom version of the razor engine so maybe this limitation can be removed. – biofractal Mar 12 '13 at 16:10
  • We're not using a custom version, just a custom build of it for licensing reasons, we haven't touched the code. – Steven Robbins Mar 13 '13 at 06:29

0 Answers0