正确注册的JavaScript和CSS在MVC 2编辑器模板 [英] Properly registering JavaScript and CSS in MVC 2 Editor Templates

查看:133
本文介绍了正确注册的JavaScript和CSS在MVC 2编辑器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何正确地在ASP.NET MVC 2(RTM)编辑模板的javascript登记块?

我在具体的情况是,我想用 Dynarch JSCal2的DateTimePicker 我的标准的datetime选择器,但这个问题是一般于任何可重用的JavaScript包。我现在我的模板正常工作,但它有我的JS和CSS包括在我的母版页,我宁愿只包含这些东西如果我真的需要他们:

 <链接rel =stylesheet属性类型=文/ CSS的href =../../内容/ JSCal2-1.7 / jscal2.css/>
<链接rel =stylesheet属性类型=文/ CSS的href =../../内容/ JSCal2-1.7 /边境radius.css/>
<脚本类型=文/ JavaScript的SRC =../../脚本/ JSCal2-1.7 / jscal2.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =../../脚本/ JSCal2-1.7 /郎/ en.js>< / SCRIPT>

所以,很显然,我可以只是把这些行到我的模板,但如果我有5 DateTimePickers的屏幕,那么这个内容将会重复5次,这不是很理想。不管怎样,我还是希望我的视图的模板来触发此code被放进<头方式> 我的网页

虽然这是完全无关的我问这个问题,我想我会分享我的模板放在这里(到目前为止)的情况下,它以任何方式有用:

 <%@控制语言=C#继承=System.Web.Mvc.ViewUserControl<&日期时间GT; %GT;<%= Html.TextBoxFor(型号=>产品型号)%GT;
<输入类型=按钮ID =<%= ViewData.TemplateInfo.GetFullHtmlFieldId(CAL触发)%>中值=.../><脚本类型=文/ JavaScript的>
    VAR<%= ViewData.TemplateInfo.GetFullHtmlFieldId(CAL)%GT; = Calendar.setup({
        触发:<%= ViewData.TemplateInfo.GetFullHtmlFieldId(的String.Empty)%GT;,
        inputField:&下;%= ViewData.TemplateInfo.GetFullHtmlFieldId(的String.Empty)%>中,
        ONSELECT:功能(){this.hide(); },
        的showTime:12,
        selectionType:Calendar.SEL_SINGLE,
        DATEFORMAT:'%O /%E /%Y%L:%M%P'
    });
< / SCRIPT>


解决方案

我通常在我的母版页由意见重写添加脚本占位符:

站长:

 <%@主语言=C#继承=System.Web.Mvc.ViewMasterPage%GT;
!< D​​OCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// EN
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML和GT;
< HEAD>
    <标题>< /标题>
    < ASP:的ContentPlaceHolder ID =样式=服务器/>
< /头>
<身体GT;
    < ASP:的ContentPlaceHolder ID =日程地址搜索Maincontent=服务器/>
    <脚本类型=文/ JavaScript的SRC =scriptAvailableToAllPages.js>< / SCRIPT>
    < ASP:的ContentPlaceHolder ID =脚本=服务器/>
< /身体GT;
< / HTML>

查看:

 <%@页面语言=C#
         的MasterPageFile =〜/查看/共享/的Site.Master
         继承=System.Web.Mvc.ViewPage&所述; Ns.MyModel>中%GT;< ASP:内容ID =indexScriptsContentPlaceHolderID =脚本=服务器>
    <脚本类型=文/ JavaScript的SRC =specific.js>< / SCRIPT>
< / ASP:内容>< ASP:内容ID =日程地址搜索MaincontentContentPlaceHolderID =日程地址搜索Maincontent=服务器>
    &下;使用%(Html.BeginForm()){%GT;
        &所述;%= Html.EditorFor(X => x.Date1)%GT;
        &所述;%= Html.EditorFor(X => x.Date2)%GT;
        &所述;%= Html.EditorFor(X => x.Date3)%GT;
        <输入类型=提交VALUE =OK/>
    <%}%GT;
< / ASP:内容>

注意编辑模板是如何包括为模型的三个不同的特性,但所需的脚本被包括一次。

How do I properly register javascript blocks in an ASP.NET MVC 2 (RTM) Editor template?

The specific scenario I'm in is that I want to use Dynarch JSCal2 DateTimePicker for my standard datetime picker, but this question is in general to any reusable javascript package. I have my template working properly now but it has my JS and CSS includes in my master page and I would rather only include these things if I actually need them:

<link rel="stylesheet" type="text/css" href="../../Content/JSCal2-1.7/jscal2.css" />
<link rel="stylesheet" type="text/css" href="../../Content/JSCal2-1.7/border-radius.css" />
<script type="text/javascript" src="../../Scripts/JSCal2-1.7/jscal2.js"></script>
<script type="text/javascript" src="../../Scripts/JSCal2-1.7/lang/en.js"></script>

So obviously I could just put these lines into my template, but then if I have a screen that has 5 DateTimePickers, then this content would be duplicated 5 times which wouldn't be ideal. Anyways, I still want my View's Template to trigger this code being put into the <head> of my page.

While it is completely unrelated to my asking this question, I thought I'd share my template on here (so far) in case it's useful in any way:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>

<%= Html.TextBoxFor(model => Model) %>
<input type="button" id="<%= ViewData.TemplateInfo.GetFullHtmlFieldId("cal-trigger") %>" value="..." />

<script type="text/javascript">
    var <%= ViewData.TemplateInfo.GetFullHtmlFieldId("cal") %> = Calendar.setup({
        trigger       : "<%= ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>",
        inputField    : "<%= ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>",
        onSelect      : function() { this.hide(); },
        showTime      : 12,
        selectionType : Calendar.SEL_SINGLE,
        dateFormat    : '%o/%e/%Y %l:%M %P'
    });
</script>

解决方案

I usually add a script placeholder in my master page that is overridden by views:

Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title></title>
    <asp:ContentPlaceHolder ID="Styles" runat="server" />
</head>
<body>
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    <script type="text/javascript" src="scriptAvailableToAllPages.js"></script>
    <asp:ContentPlaceHolder ID="Scripts" runat="server" />
</body>
</html>

View:

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

<asp:Content ID="indexScripts" ContentPlaceHolderID="Scripts" runat="server">
    <script type="text/javascript" src="specific.js"></script>
</asp:Content>

<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm()) { %>
        <%= Html.EditorFor(x => x.Date1) %>
        <%= Html.EditorFor(x => x.Date2) %>
        <%= Html.EditorFor(x => x.Date3) %>
        <input type="submit" value="OK" />
    <% } %>
</asp:Content>

Notice how the editor template is included for three different properties of the model but the required scripts are included only once.

这篇关于正确注册的JavaScript和CSS在MVC 2编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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