ASP.NET MVC 3 How to inject Javascript into the master layout - Stack Overflow

I am trying out the new razor view engine from MVC 3.The issue that I am having is that I have Javasc

I am trying out the new razor view engine from MVC 3. The issue that I am having is that I have Javascript that is page specific. I normally have all my Javascript code before the tag closes. I was thinking of putting a section just before I close the body tag on my master layout. Some thing to the effect of:

<script type="text/javascript">
   @RenderSection("JavaScript")
</script>

But VS2010 underlines it in green. So which ever pages uses this master layout can have it's Javascript injected Here. How would you guys do it? The reason why I want to do it like this is because then I can add JavaScript from the master layout also in here, other I will have to define a separate script tag just below the @RenderSection.

When I do the following then VS gives me a warning (I don't like warnings):

Validation (HTML 4.01): Element 'link' cannot be nested within element 'link'.

Here is my code for the above warning:

@section HeadSection
{
    <link href=".8.2r1/build/button/assets/skins/sam/button.css" rel="stylesheet" type="text/css">
    <link href=".8.2r1/build/datatable/assets/skins/sam/datatable.css" rel="stylesheet" type="text/css">
}

How can I get these warnings away?

I am trying out the new razor view engine from MVC 3. The issue that I am having is that I have Javascript that is page specific. I normally have all my Javascript code before the tag closes. I was thinking of putting a section just before I close the body tag on my master layout. Some thing to the effect of:

<script type="text/javascript">
   @RenderSection("JavaScript")
</script>

But VS2010 underlines it in green. So which ever pages uses this master layout can have it's Javascript injected Here. How would you guys do it? The reason why I want to do it like this is because then I can add JavaScript from the master layout also in here, other I will have to define a separate script tag just below the @RenderSection.

When I do the following then VS gives me a warning (I don't like warnings):

Validation (HTML 4.01): Element 'link' cannot be nested within element 'link'.

Here is my code for the above warning:

@section HeadSection
{
    <link href="http://yui.yahooapis./2.8.2r1/build/button/assets/skins/sam/button.css" rel="stylesheet" type="text/css">
    <link href="http://yui.yahooapis./2.8.2r1/build/datatable/assets/skins/sam/datatable.css" rel="stylesheet" type="text/css">
}

How can I get these warnings away?

Share Improve this question edited Dec 12, 2010 at 8:17 Brendan Vogt asked Dec 11, 2010 at 19:13 Brendan VogtBrendan Vogt 26.1k39 gold badges150 silver badges235 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Here's what I'd do, according to the best practices you should place your scripts at the bottom of the page.

_Layout.cshtml

<html>
    <head>
        @* ... *@
    </head>
    <body>
        @* ... *@        
        @RenderSection("Scripts", false)
    </body>
</html>

MyView.cshtml

@section Scripts
{
    <script src="@Url.Content("~/Scripts/myScript.js")"
            type="text/javascript"></script>
}

I would use @RenderSection("head", false) in my _layout page. Then you can inject whatever you want in the head of the page (including script)...and by using false you make it optional on all of your view pages.

You can turn off or modify VS's XHTML validation checking: http://weblogs.asp/scottgu/archive/2005/11/23/431350.aspx

You need to close the link tags.

Like this:

@section HeadSection
{
    <link href="http://yui.yahooapis./2.8.2r1/build/button/assets/skins/sam/button.css" rel="stylesheet" type="text/css" />
    <link href="http://yui.yahooapis./2.8.2r1/build/datatable/assets/skins/sam/datatable.css" rel="stylesheet" type="text/css" />
}

And then follow Ryan's answer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743767590a4503780.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信