質問

があるので、追加のCSSへの参照ページからカスタマーサポート, して描画するページの <head> (必要に HTML4.01仕様)?

役に立ちましたか?

解決 4

あなたは、応答HTMLを操作し、適切な場所に任意のCSS /スクリプト参照を移動するためにHttpModuleを使用することができます。これは理想的ではない、と私はパフォーマンスへの影響のわからないんだけど、MVCの原則に対する作業()JavaScriptベースのソリューション、または(b)のいずれかなしに問題を解決する唯一の方法のように思えます。

他のヒント

使用している場合は、MVC3&カミソリ、最良の方法の追加毎にページの項目をご紹介す:1)電話RenderSection()内からレイアウトページ 2)を宣言するのに対応する部内でお子さんのページ:

/ビング/共有/_Layout.cshtml:

<head>
    <!-- ... Rest of your head section here ... ->
    @RenderSection("HeadArea")
</head>

/Views/作品/Index.cshtml:

@section HeadArea {
    <link rel="Stylesheet" type="text/css" href="/Entries/Entries.css" />
}

それをHTMLページにそれまで見えるようになります:

<head>
    <!-- ... Rest of your head section here ... ->
    <link rel="Stylesheet" type="text/css" href="/Entries/Entries.css" />
<head>

また、MVCのためのTelerikオープンソースコントロールを使用してのような何かを行うことができます:

<%= Html.Telerik().StyleSheetRegistrar()
                  .DefaultGroup(group => group
                     .Add("stylesheet.css"));
ヘッド部では

そして、

<%= Html.Telerik().ScriptRegistrar()
                  .DefaultGroup(group => group
                     .Add("script.js"));
あなたのページのbotttomのスクリプトセクションで

そして、あなたは、任意のビューでスクリプトを追加し続ける、または部分的なビューと、彼らが動作する必要がありますすることができます。

あなたがコンポーネントを使用したくない場合は、

あなたは常にそこから自分を鼓舞し、より多くのカスタム何かを行うことができます。

ああ、Telerikであなたもスクリプトを組み合わせると圧縮のオプションがあります。

あなたは頭にスタイルに低下ジャバスクリプトブロック内の部分図の負荷を持つことができ、それは愚かなあなたはおそらく同じ理由のためのヘッド部内のJavaScriptブロックをしたいことを考慮されるだろう。

私は最近、かなりクールしかし、何かを発見しました。あなたは、文字列に部分的なビューをシリアライズし、JSONオブジェクトの一部として、それをクライアントに送り返すことができます。これは、ビューと一緒に、同様に他のパラメータを渡すことができます。

あなたは、jQueryとAjaxを使ってJSONオブジェクトを取得し、それが部分図でロードされているし、別のJSONプロパティは、あなたのスタイルブロックすることができことができます。あなたのスタイルブロックを返した場合ので、その後、ヘッド部にドロップした場合はJQueryは、チェックすることができます。

のような何かます:

$.ajax(
{
     url: "your/action/method",
     data: { some: data },
     success: function(response)
     {
          $('#partialViewContainer).html(response.partialView);
          if (response.styleBlock != null)
               $('head').append(response.styleBlock);
     }
});
MVCの原則を破る別のアプローチは、すなわちmyViewModel.Css.Add(「CSS」)と、あなたの中で(希望CSS / JavaScriptを設定するには、ページの初期化イベントにViewModelにし、応答を使用することです頭部あなたのviewmodel上のCSS-コレクションのコンテンツをレンダリングます。

あなたが基本のviewmodelクラスを作成し、これを行うためにはそれとは、すべての他のモデルの継承、ALA

public class BaseViewModel
{
    public string Css { get; set; }
}

あなたのマスターページでは、このviewmodelの

を使用するように設定しました
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<BaseViewModel>" %>

と、あなたの頭のセクションでは、CSSプロパティ

の値を書き出すことができます
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

    <%= Model.Css %>
</head>

さて、あなたの部分図であなたは、MVCでちょっと醜いです、このコードを、持っている必要があります。

<script runat="server">
    protected override void OnInit(EventArgs e)
    {
        Model.Css = "hej";

        base.OnInit(e);
    }
</script>

以下では、JavaScriptが有効になった場合にのみ動作します。それは私はあなたが言及して正確なシナリオのために使用することはほとんどのヘルパーです。

// standard method - renders as defined in as(cp)x file
public static MvcHtmlString Css(this HtmlHelper html, string path)
{
    return html.Css(path, false);
}
// override - to allow javascript to put css in head
public static MvcHtmlString Css(this HtmlHelper html, 
                                string path, 
                                bool renderAsAjax)
{
    var filePath = VirtualPathUtility.ToAbsolute(path);

    HttpContextBase context = html.ViewContext.HttpContext;
    // don't add the file if it's already there
    if (context.Items.Contains(filePath))
        return null;

    // otherwise, add it to the context and put on page
    // this of course only works for items going in via the current
    // request and by this method
    context.Items.Add(filePath, filePath);

    // js and css function strings
    const string jsHead = "<script type='text/javascript'>";
    const string jsFoot = "</script>";
    const string jsFunctionStt = "$(function(){";
    const string jsFunctionEnd = "});";
    string linkText = string.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\"></link>", filePath);
    string jsBody = string.Format("$('head').prepend('{0}');", linkText);

    var sb = new StringBuilder();

    if (renderAsAjax)
    {
        // join it all up now
        sb.Append(jsHead);
        sb.AppendFormat("\r\n\t");
        sb.Append(jsFunctionStt);
        sb.AppendFormat("\r\n\t\t");
        sb.Append(jsBody);
        sb.AppendFormat("\r\n\t");
        sb.Append(jsFunctionEnd);
        sb.AppendFormat("\r\n");
        sb.Append(jsFoot);
    }
    else
    {
        sb.Append(linkText);
    }

    return MvcHtmlString.Create( sb.ToString());
}

使用方法:

<%=Html.Css("~/content/site.css", true) %>
これはほとんどその有用性を制限し、ジャバスクリプトが有効になっている場合にのみ、述べたようにカントー

は、私のために動作します。

scroll top