jquery - 在asp.net mvc中使用jQuery datepicker的问题

标签 jquery asp.net asp.net-mvc jquery-ui-datepicker

我想将 Jquery 日期选择器添加到 TimeofCreation 表单中。我无法获取日期选择器或选择按钮。请帮助使用 jquery 的日期选择器。

我希望当用户单击“TimeofCreation”表单时显示日期日历。

捐赠者类代码如下

   public class Donor
   {
      [Key]
     public int Id { get; set; }
     public string FullName { get; set; }
     public int PhoneNumber { get; set; }
     public DateTime TimeofCreation { get; set; }
     public string Emergency { get; set; }
     public string Address { get; set; }
     public BloodType BloodList    { get; set; }
     public string BagsNumber { get; set; }
     public DateTime LastTimeDonation { get; set; }

   }

我的查看代码如下。

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-group">
    @Html.LabelFor(model => model.TimeofCreation, htmlAttributes: new { 
    @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.TimeofCreation, new { 
        htmlAttributes = new { @class = "datepicker" } })
        @Html.ValidationMessageFor(model => model.TimeofCreation, "", new 
       { @class = "text-danger" })
        </div>

        </div>


@section Scripts {
    <script src="~/assets/plugins/jquery-ui/jquery-ui.min.js"></script>
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")


    <script type="text/javascript">
         <script>

    $('.datepicker').datepicker({
        dateFormat: "dd/M/yy",
        changeMonth: true,
        changeYear: true,
        yearRange: "-60:+0"
    });
</script>
    </script>

}

最佳答案

您遇到的问题是未添加 jquery-ui.css 文件,其他问题是您多次上传 jqueryjquery-ui > js 文件。使用外部脚本 js 文件或使用 bundle 。

而且您的外部脚本文件的顺序也是错误的,它应该是使用 jquery-ui 文件之后的第一个 jquery

<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/assets/plugins/jquery-ui/jquery-ui.min.js"></script>

例如,我使用了这个并删除了 bundle 。

并添加您的jquery-ui.css文件

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

之后您可以看到您的日期选择器

更改后的代码如下所示

@model MVC5.Models.Donor
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-group">
            @Html.LabelFor(model => model.TimeofCreation, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TimeofCreation, new { htmlAttributes = new { @class = "datepicker" } })
                @Html.ValidationMessageFor(model => model.TimeofCreation, "", new { @class = "text-danger" })
            </div>

        </div>
    }

    @section Scripts {
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/assets/plugins/jquery-ui/jquery-ui.min.js"></script>
        <script type="text/javascript">

            $('.datepicker').datepicker({
                dateFormat: "dd/M/yy",
                changeMonth: true,
                changeYear: true,
                yearRange: "-60:+0"
            });
        </script>
    }

enter image description here

我希望这对您有所帮助。如果需要更多信息,请告诉我。

关于jquery - 在asp.net mvc中使用jQuery datepicker的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56012782/

相关文章:

asp.net-mvc - 服务层或存储库中的密码哈希等?

javascript - 如何在文档准备好之前强制从 js 准备好边距

javascript - 列出未使用 .val 函数显示的字段内容

css - Jquery 和 css 引用在 IE7 兼容性上阻止 Asp.net 中的页面呈现

asp.net - 缺少 IUSR 帐户和 Asp.net 帐户

c# - 如果 HTTP 是无状态的,那么 ASP.NET MVC 如何支持 session ?

asp.net-mvc - 无法通过剑道上传将附件从一个 View 移动到另一个 View

c# - 为什么我的 Controller 没有填充 Action 参数?

javascript - 为什么它不断重复我正在更新的当前行?

jquery - 如何将 ViewModel bool 值传递给 jquery 函数?