.Net Core 2.0 MVC 中 JQuery DatePicker 的问题

Trouble with JQuery DatePicker in .NetCore 2.0 MVC

我似乎很难让 Jquery 日期选择器在我的基本 CRUD 应用程序中工作。我已经使用 NuGet 包管理器安装 Jquery UI.Combined 库:

https://ibb.co/mmEfic

在我的 _Layout.cshtml 文件中,我有以下脚本标签:

    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>

(我的 ViewStart 文件中引用了 _Layout.cshtml)

最后,我尝试在我的视图中使用日期选择器:

@model GymTracker.ViewModel.MemberViewModel

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

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">



<h2>Create</h2>

<h4>Member</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DateOfBirth" class="control-label"></label>

           @Html.TextBox("datetimepicker")


            </div>
            <div class="form-group">
                <label asp-for="PaymentType" class="control-label"></label>
                @Html.DropDownListFor(model => model.PaymentType, Html.GetEnumSelectList(typeof(Membershiptype)))

                <span asp-validation-for="PaymentType" 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");}
}

谁能看出我做错了什么。当然是行 :

@Html.TextBox("datetimepicker")

是否会呈现 Jquery 日期选择器?

如有任何帮助,我们将不胜感激。谢谢

我遇到了同样的问题,但从未找到 "clean" 解决方案 - 但这个解决方法对我有用:

放置一个简单的 div 和输入字段,然后隐藏实际的表单字段(调整空格对齐):

<div>
            &nbsp;  &nbsp; Ride Date <br />
            &nbsp;  &nbsp; <input type="text" id="datepicker">
        </div>

        <div class="form-group d-none">
            <label asp-for="RideDate" class="col-md-6 control-label"></label>
            <div class="col-md-6">
                <input asp-for="RideDate" class="form-control" />
                <span asp-validation-for="RideDate" class="text-danger"></span>
            </div>
            <br />
        </div>

在提交上添加 ID:

        <div class="row justify-content-md-center">
            <div class="form-group">
                <div class="col-md-5">
                    <button id="fixtime" type="submit" class="btn btn-primary">Add Comment</button>
                </div>
            </div>
        </div>

然后将用户选择的日期移动到实际输入字段:

    <script>
        $(document).ready(function () {
            $('#datepicker').datepicker({
                uiLibrary: 'bootstrap4'
            });
            //load current date into datapicker

            var now = new Date();
            var day = ("0" + now.getDate()).slice(-2);
            var month = ("0" + (now.getMonth() + 1)).slice(-2);
            var today = month + '/' + day + '/' + now.getFullYear();
            //alert(today);
            $('#datepicker').val(today);

            //$("#RideDate").val(now.getFullYear() + "-" + month + "-" + day);  // not needed

            $("#fixtime").click(function () {
                var fix = $("#datepicker").val(); // from datepicker mm/dd/yyyy to ASP yyyy-mm-dd
                var fix2 = fix.substr(6) + "-" + fix.substr(0, 2) + "-" + fix.substr(3, 2);
                $("#RideDate").val(fix2);
                //alert("ride date = " +  $("#RideDate").val());
            });
        });
    </script>

不是很优雅,但工作正常。米