如何向ASP中添加JavaScript。净MVC视图?

时间:2021-10-31 06:36:27

I have a simple View and I wish to add a JQuery DatePicker JavaScript to this view (and not every view, via a masterpage).

我有一个简单的视图,我希望向这个视图添加JQuery DatePicker JavaScript(不是通过一个主页添加每个视图)。

I'm not sure what is the best way to do this.

我不知道最好的办法是什么。

Second, I'm conscious of where/when my JavaScript loads. I'm a fan of YSlow and it recommends that I add any scripts to the bottom of the page, which I do.

其次,我知道JavaScript加载的位置/时间。我是YSlow的粉丝,它建议我在页面底部添加任何脚本,我就是这么做的。

So, how could I do both?

那么,我怎么能同时做到这两点呢?

Here's the view:

视图:

<%@ Page
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <% using (Html.BeginForm()) {%>

    <p>
        <label for="StartDate">Start Date:</label>
        <!-- JQuery DatePicker to be added, here. -->
    </p>
    <% } %>
</asp:Content>

3 个解决方案

#1


12  

One way is put a content placeholder at the bottom of your master page, then the specific slave page that needs the javascript can specify the placeholders contents as the required javascript. Other pages that don't need the javascript simply don't specify any content for the placeholder.

一种方法是在主页的底部放置一个内容占位符,然后需要javascript的特定从属页面可以将占位符内容指定为所需的javascript。其他不需要javascript的页面则不为占位符指定任何内容。

#2


22  

In ASP.NET MVC3, you can now use a RenderSection to achieve this, at least for simpler scenarios. Just add a RenderSection to the bottom of the layout page, and from your view fill in the appliation code

在ASP。NET MVC3,您现在可以使用RenderSection来实现这一点,至少对于更简单的场景是如此。只需在布局页面的底部添加一个RenderSection,然后从视图中填充应用代码

RenderSections: http://www.dotnetcurry.com/ShowArticle.aspx?ID=636

RenderSections:http://www.dotnetcurry.com/ShowArticle.aspx?ID=636

#3


15  

Your MasterPage should have a ContentPlaceHolder for the head.

您的MasterPage应该有一个用于头部的ContentPlaceHolder。

<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server" />
</head>

Then you're able to include head elements (JavaScripts, StyleSheets, etc...) in your views:

然后你可以在你的视图中包含标题元素(javascript、样式表等):

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="/Scripts/jquery/ui/ui.datepicker.js" type="text/javascript">
    </script>
    <link href="/Styles/myStyle.css" rel="stylesheet" type="text/css" />
</asp:Content>

#1


12  

One way is put a content placeholder at the bottom of your master page, then the specific slave page that needs the javascript can specify the placeholders contents as the required javascript. Other pages that don't need the javascript simply don't specify any content for the placeholder.

一种方法是在主页的底部放置一个内容占位符,然后需要javascript的特定从属页面可以将占位符内容指定为所需的javascript。其他不需要javascript的页面则不为占位符指定任何内容。

#2


22  

In ASP.NET MVC3, you can now use a RenderSection to achieve this, at least for simpler scenarios. Just add a RenderSection to the bottom of the layout page, and from your view fill in the appliation code

在ASP。NET MVC3,您现在可以使用RenderSection来实现这一点,至少对于更简单的场景是如此。只需在布局页面的底部添加一个RenderSection,然后从视图中填充应用代码

RenderSections: http://www.dotnetcurry.com/ShowArticle.aspx?ID=636

RenderSections:http://www.dotnetcurry.com/ShowArticle.aspx?ID=636

#3


15  

Your MasterPage should have a ContentPlaceHolder for the head.

您的MasterPage应该有一个用于头部的ContentPlaceHolder。

<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server" />
</head>

Then you're able to include head elements (JavaScripts, StyleSheets, etc...) in your views:

然后你可以在你的视图中包含标题元素(javascript、样式表等):

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="/Scripts/jquery/ui/ui.datepicker.js" type="text/javascript">
    </script>
    <link href="/Styles/myStyle.css" rel="stylesheet" type="text/css" />
</asp:Content>