在.NET中使用DatePicker [英] Using DatePicker with .NET

查看:45
本文介绍了在.NET中使用DatePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET Core创建了一个非常简单的DatePicker.这是我的模型:

I have created a very simple DatePicker using .NET Core. Here is my model:

public class MemberViewModel
    {
        public string Name { get; set; }
        [DisplayFormat(DataFormatString = @"{0:dd\/MM\/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DOB { get; set; }
    }

这是我的观点:

@model DatePicker.Models.MemberViewModel

    @{
        ViewData["Title"] = "MemberView";
    }

    <h2>MemberView</h2>

    <h4>MemberViewModel</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="MemberView">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DOB" class="control-label"></label>
                    @Html.TextBox("datepicker")
                    @*<input asp-for="DOB" class="form-control" />
                        <span asp-validation-for="DOB" class="text-danger"></span>*@
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </form>
        </div>
    </div>

    <div>
        <a asp-action="Index">Back to List</a>
    </div>

    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }

这是我的布局:

<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Datepicker - Restrict date range</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $(function () {
            $("#datepicker").datepicker({ minDate: -20, maxDate: "+1M +10D", dateFormat: "dd/mm/yy" });
        });
    </script>

当我在所有浏览器中单击文本框时,DatePicker不执行任何操作.奇怪的是;如果我将生成的HTML复制并粘贴到HTML文件中,然后双击该HTML文件,则DatePicker会按预期工作.有什么问题吗?

The DatePicker does nothing when I click on the textbox (in all browsers). The strange thing is; if I copy and paste the HTML generated into a HTML file and then double click on the HTML file, then the DatePicker works as expected. What is the problem?

更新

请查看下面生成的HTML:

Please see the HTML generated below:

<!DOCTYPE html>
<html>
<head>



    <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Datepicker - Restrict date range</title>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>
        $(function () {
            $("#datepicker").datepicker({ minDate: -20, maxDate: "+1M +10D", dateFormat: "dd/mm/yy" });
        });
    </script>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MemberView - DatePicker Hotla</title>

</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/">DatePicker</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="/">Home</a></li>
                    <li><a href="/Home/About">About</a></li>
                    <li><a href="/Home/Contact">Contact</a></li>
                </ul>
            </div>
        </div>
    </nav>





    <div class="container body-content">


<h2>MemberView</h2>

<h4>MemberViewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form action="/Home/MemberView" method="post">

            <div class="form-group">
                <label class="control-label" for="Name">Name</label>
                <input class="form-control" type="text" id="Name" name="Name" value="" />
                <span class="text-danger field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label class="control-label" for="DOB">DOB1</label>
                <input id="datepicker" name="datepicker" type="text" value="" />
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        <input name="__RequestVerificationToken" type="hidden" value="CfDJ8AZxPMp7tE9EgVGFaTl7fvF-ALxvEVCrHkYj_isQTt5HUG2iu3EPMhNjR_6OlI2xtybr9grj4e4c3eFwe5jY-8r5YWeWIdtSNkc5IsvYnt9JYc5ftpPSzAqzINzih1yg85DFHJLEkWRZ2EjvupuQQ5E" /></form>
    </div>
</div>

<div>
    <a href="/">Back to List</a>
</div>


        <hr />
        <footer>
            <p>&copy; 2018 - DatePicker</p>
        </footer>
    </div>


        <script src="/lib/jquery/dist/jquery.js"></script>
        <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script>

    <script src="/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>

</body>
</html>

如果我注释掉以下行,那么它将按预期工作:

If I comment out the following line, then it works as expected:

<script src="/lib/jquery/dist/jquery.js"></script>

推荐答案

您要包括jQuery 两次.第二个jQuery包含项将覆盖您的$变量,删除您事先加载的jQuery ui插件.

You are including jQuery twice. The second jQuery inclusion overwrites your $ variable, removing the jQuery ui plugins you loaded beforehand.

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<script src="/lib/jquery/dist/jquery.js"></script>

jQuery插件通过扩展$变量来工作.您第二次包含jQuery会破坏先前加载的插件.

jQuery plugins work by extending the $ variable. You second inclusion of jQuery destroys the plugins previously loaded.

这篇关于在.NET中使用DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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