【问题标题】:Bootstrap datepicker not working in MVC project引导日期选择器在 MVC 项目中不起作用
【发布时间】:2018-01-25 00:57:04
【问题描述】:

我遵循了这家伙的第三个解决方案:

https://blog.jigsawpieces.me/2014/07/23/lbd-adding-datetimepicker-control-to-mvc-project/

据说通过 Visual Studio 中的 Nuget 包管理器安装:

Install-Package Bootstrap.v3.Datetimepicker

然后将其添加到 Bundle.config:

bundles.Add(new ScriptBundle("~/bundles/moment").Include(
                    "~/Scripts/moment.js"));

我看到它在输入字段旁边显示日历图标,但是当我单击它时,日历不会下拉。这是我的 Bundle.config 文件:

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery-ui*"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/moment").Include(
                   "~/Scripts/moment.js"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css", 
                  "~/Content/themes/base/jquery.ui.all.css",
                  "~/Content/site.css"));


    }

这是显示此输入字段的视图:

<div class="row">
                            <div class='col-sm-6'>
                                <div class="form-group">
                                    <div class='input-group date' id='datetimepicker1'>
                                        <input type='text' class="form-control datepicker" />
                                        <span class="input-group-addon">
                                            <span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <script type="text/javascript">
                                    $(function () {
                                        $('#datetimepicker1').datetimepicker({ format: 'dd MM yyyy - hh:ii' });
                                    });
                            </script>
                        </div>

我做错了什么?为什么日历不会下拉并显示日期选择器?

这是我的新 Bundle.config。我添加了其他 js 和 css 文件,但仍然没有:

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js",
                    "~/Scripts/jquery-ui*"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/moment").Include(
                   "~/Scripts/moment.js"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/bootstrap-datetimepicker.min.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/bootstrap-datetimepicker.min.css",
                  "~/Content/themes/base/jquery.ui.all.css",
                  "~/Content/site.css"));


    }

【问题讨论】:

    标签: jquery asp.net-mvc twitter-bootstrap datepicker


    【解决方案1】:

    您缺少 datetimepicker 资源。

    来自您发布的博客:

    运行此命令会将新版本的 bootstrap-datetimepicker.js 和 bootstrap-datetimepicker.css 添加到我的项目中。我唯一需要做的额外配置就是将 moment.js 添加到 BundleConfig.cs 文件中

    前面的步骤添加了这些文件。

    我创建了一个新的 MVC 项目并使其正常工作。

    BundleConfig.cs

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
    
        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));
    
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));
    
        bundles.Add(new ScriptBundle("~/bundles/moment").Include(
                    "~/Scripts/moment.js"));
    
        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js",
                  "~/Scripts/bootstrap-datetimepicker.js"));
    
        bundles.Add(new StyleBundle("~/Content/css").Include(
                 "~/Content/bootstrap.css",
                 "~/Content/bootstrap-datetimepicker.css",
                  "~/Content/site.css"));
    }
    

    _Layout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - My ASP.NET Application</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <div class="container body-content">
            @RenderBody()
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/moment")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
    

    请注意,Moment 需要在 datetimepicker 之前加载。 Moment 需要单独下载。

    Index.cshtml

    <div class="row">
        <div class="col-md-4">
            <input type="text" id="datetimepicker1" />
        </div>
    </div>
    
    @section scripts {
        <script>
            $("#datetimepicker1").datetimepicker();
        </script>
    }
    

    【讨论】:

    • 所以我应该将 bootstrap-datetimepicker.js 和 bootstrap-datetimepicker.css 添加到我的 Bundle.config 中?
    • 这是我的新 bundle.config 文件。它仍然没有做任何不同的事情。 :(我不明白为什么。我在我的问题中附加了上面的新 bundle.config。@Jasen
    • 尝试删除 jQuery-UI。另外,尝试从.datetimepicker() 中删除所有选项。检查页面源以确保包含脚本和样式表。在调试控制台中检查脚本错误。
    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多