3

i thing i put something wrong here sorry for that if you confused.

i want to put Javascript or Css in the Head of my page [who is render in browser]. when i try to put them then i found that he inside of Body not inside of head tag.

i know that i can easily put in head if i not inherit them from master page.

but how i can put on my page if i inherit them from master page.

i want a sollution for MVC 3 project and my page is written using Razor viewenzine.

2 Answers 2

8

In the head section of your layout page, add a

RenderSection("head")

call. Then, in your Razor view, add

@section head {
 @myincludemarkup
}

Where "myincludemarkup" of course is the html markup of your script/stylesheet references.

edit:

The section in your layout page (master page) could look something like this:

<head>
@RenderSection("head")
</head>

This would force each and everyone of your views to define a section called "head" by writing the code at the top of my answer, @section etc.

If you want the section optional in your viewpages, you can write

<head>
@RenderSection("head", optional:true)
</head>

Use this page for reference:

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

edit: to use your own code:

@inherits System.Web.Mvc.WebViewPage<dynamic>

@using Razor
@using Razor.Models


@{
    View.Title = "Index";
    LayoutPage = "~/Views/Shared/_Layout.cshtml";
}

<h2>@View.Message</h2>

@{
    UserManager.product prod =  UserManager.getUserinfo();
 }
@prod.Price
@prod.Title
@prod.ID
<h2></h2>

@section head {
 @myincludemarkup
}
Sign up to request clarification or add additional context in comments.

4 Comments

when i co this i got the error " Section not defined: 'head'"
Well, you need to define the section in your view. See the web page in my answer for more information. Remember, you need to do this in every page. I'll update my answer with information on how to do it for only select pages.
Adding the code as an "answer" to your own question is not the right way though. Edit your question and add it instead. :)
Sorry, I managed to introduce a typo that has now been fixed. It should be @section head, not @section menu. Sorry.
1

Place a content area in the head section. Then you can add code there in any content page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.