Mvc4 datatables does not work in script bundle

白昼怎懂夜的黑 提交于 2019-12-12 05:39:27

问题


I have following code in the App_Start/BundleConfig.cs

    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

    bundles.Add(new ScriptBundle("~/bundles/datatable").Include(
                    "~/Content/DataTables-1.10.7/media/js/jquery.dataTables.min.js"));

    bundles.Add(new StyleBundle("~/Content/css").Include(
                    "~/Content/site.css", 
                    "~/Content/DataTables-1.10.7/media/css/jquery.dataTables.min.css"));

and in the Views/Shared/_Layout.cshtml

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/datatable")

and CSS and Datatable just does not work, but when I add manualy

<link href='@Url.Content("~/Content/DataTables-1.10.7/media/css/jquery.dataTables.min.css")' rel="stylesheet" />

<script src='@Url.Content("~/Content/DataTables-1.10.7/media/js/jquery.dataTables.min.js")' type="text/javascript"></script>

it starts to work. What am I doing wrong ?


回答1:


Inspect the source code of the web page from the browser and be sure that;

jquery.dataTables.min.js is loading after jquery.min.js

Change the order in your _Layout.cshtml as:

@Scripts.Render("~/bundles/jquery")    
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/datatable")

EDIT :

It may also be about web.config settings.

If you have "Debug = True" in your web.config, minimized javascript files are not include in bundles.

Add this to your BundleConfig.cs :

#if DEBUG
        //by default all minimized files are ignored in DEBUG mode. This will stop that.
        bundles.IgnoreList.Clear();
#endif


来源:https://stackoverflow.com/questions/36356085/mvc4-datatables-does-not-work-in-script-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!