如何添加JavaScript来一个ASP.NET MVC视图? [英] How do I add JavaScript to an ASP.NET MVC View?

查看:127
本文介绍了如何添加JavaScript来一个ASP.NET MVC视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的查看和我想一个的JQuery的DatePicker的JavaScript添加这个观点(与不是的每个的视图,通过一个母版)。

我不知道什么是做到这一点的最好办法。

二,我有意识的在那里/当我的JavaScript负载。我YSlow的的粉丝,并建议我添加任何脚本,到页面的底部,这是我做的。

所以,我怎么能做到两者兼而有之?

下面的观点:

<%@页
    语言=C#
    的MasterPageFile =〜/查看/共享/的Site.Master
    继承=System.Web.Mvc.ViewPage%GT;< ASP:内容ID =内容1ContentPlaceHolderID =日程地址搜索Maincontent=服务器>    < H2>指数< / H>    &下;使用%(Html.BeginForm()){%GT;    &所述p为H.;
        <标签=起始日期>起始日期:< /标签>
        <! - 无以复加的JQuery的DatePicker,在这里。 - >
    &所述; / P>
    <%}%GT;
< / ASP:内容>


解决方案

一种方法是把内容占位符在您的母版页,那么需要的JavaScript可以指定占位符内容所需的JavaScript中的特定的从页面底部。不需要JavaScript的其他网页根本就没有指定占位符的任何内容。

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

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.

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>

解决方案

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来一个ASP.NET MVC视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆