The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method
Asked Answered
F

2

44

I am trying to separate all the things that I could reuse in sections, so it would be easier for me to maintain.

However I got this exception: The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method

I created a file called sections.cshtml with the following content:

@section scripts{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

And in the _layout.cshtml file I changed it to:

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    @RenderSection("scripts", required:false)
    @*<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>*@
</head>

When I go to the view in the browser and check the source code it shows only:

<head>
    <meta charset="utf-8" />
    <title>Edit</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
</head>
Flummox answered 18/10, 2011 at 13:56 Comment(4)
Why not just put the scripts into your Layout file?Likeness
just trying to learn how to use the sections in mvc, but yes thats an option also, but what about if some scripts will be used on some views, and other scripts in other views? Sections would solve this but the Layout file would not solve it, right?Flummox
Get rid of @RenderSection and just put the scripts in... that is the point of the layout file to store these things that are common amongst most of your pages.Likeness
Did you put the @section scripts{} in your Edit.cshtml file? That is where you put it, not in a separate file.Bookrest
R
42

RenderSection can only exist in Layout files (i.e. master pages)... its purpose is to allow the pages you can request directly to target various sections of a Layout (layout being a file common to all pages which choose to use it) and supply content for these different sections.

If you want to separate this section out as something which is resuable on many pages you should put it in a partial and replace the rendersection call to something like

@Html.Partial("Scripts")
Refute answered 18/10, 2011 at 14:7 Comment(5)
please check the edit, I tried to do it using the layout file instead of partial views and its not working, I dont get an exception either.Flummox
If I change section requireed to true, it gives me this exception: Section not defined: "scripts", Do I need to place the section somewhere else??Flummox
That is because the file which you request directly would need to have section in it marked up as @section script{....} with the .... containing the content for what should go into the Layout file at the point where @RenderSection("scripts") is called... sections aren't for defining common functionality. That's what partials are forRefute
Sections are for defining an area where content should go in a layout page (e.g. the footer).. but that content would be different for each page you requestRefute
I have a Layout>Page>Partial. I need some scripts and styles to be referenced and loaded in the <head/> only when this partial is there, how am I supposed to this. I don'g get the point of this stupid constraint.Cumulative
A
14

Alternatively you could use helper to separate code you use more often. Especially if you cannot use sections because of the constraint Martin-Booth mentioned.

@helper Scripts(){
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

and the usage is just:

<somehtml />
@Scripts()
<somehtml />
Algoid answered 29/3, 2012 at 12:20 Comment(1)
And what if the scripts are supposed to go in the <head> Tag?Cardwell

© 2022 - 2024 — McMap. All rights reserved.