【问题标题】:bundle css file in MVC4在 MVC4 中捆绑 css 文件
【发布时间】:2013-09-28 00:35:21
【问题描述】:

我的 asp.net mvc4 项目中有一些内容,例如 css 文件。其中一个文件是与它所在的 jqGrid 相关的文件:

\Content\jquery.jqGrid\ui.jqgrid.css

所以我可以在头部标签之间使用下面的导入它:

<link href="@Url.Content("~/Content/ui.jqGrid.css")" rel="stylesheet" type="text/css" media="all"/>

但我已经看到 site.css 是使用捆绑技术导入的,所以我想在 mvc4 框架的 App_Start 文件夹下的 BundleConfig.cs 中执行相同的操作。我正在使用以下行,但它不起作用:

bundles.Add(new StyleBundle("/Content/css").Include("~/Content/jquery.jqGrid/ui.jqgrid.css"));

我想我需要包含,因为我的代码中有错误,请参见下文。

在我看来,我有以下几点:

<div id="jqGrid">
    @Html.Partial("../Component/_ComponentGrid")
</div>

我有一个 javascript 文件,开头为:

function showGrid() {
    $('#_componentGrid').jqGrid({
        caption: paramFromView.Caption, ....

在运行时引发错误:对象不接受属性或方法“jqGrid”。

有什么想法吗?

【问题讨论】:

    标签: jquery asp.net-mvc-4 jqgrid


    【解决方案1】:

    可能在“/Content”之前缺少“~”:

    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/jquery.jqGrid/ui.jqgrid.css"));
    

    &lt;head&gt; 标签之间你应该这样称呼:

    @Styles.Render("~/Content/css")
    

    并且错误:对象不接受属性或方法'jqGrid'。看来 jqGrid .js 不包括在内...

    【讨论】:

      【解决方案2】:

      向后工作并将脚本文件直接包含在您的视图中,然后在您的布局中,然后将它们添加到包中。

      当您将它们添加到包中时,您仍然需要通过以下方式将该包添加到您的视图或布局中:

      @Styles.Render("~/Content/css")
      

      您可以通过 Firebug 或其等效工具进行检查,以确保您的包/脚本正确加载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 1970-01-01
        • 2013-04-02
        • 2012-07-22
        • 1970-01-01
        相关资源
        最近更新 更多