Steven Spielberg
Steven Spielberg

Reputation:

Write css for individual page in mvc3 project

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.

Upvotes: 3

Views: 526

Answers (2)

Philip Smith
Philip Smith

Reputation: 2801

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

Upvotes: 1

Max
Max

Reputation: 4405

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
}

Upvotes: 8

Related Questions