我如何实现JqueryUI Datepicker

时间:2016-02-28 15:34:42

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

如何从JqueryUI Datepicker库中实现DateTime Picker。

到目前为止,我已经完成了这些步骤http://blog.falafel.com/three-steps-use-jquery-ui-asp-net-mvc-5/ 我已经完成了前3个步骤,现在我不知道如何继续前进,以便我可以实现DateTime Picker?

到目前为止,我编写的是模型类中的datetime变量,如下所示

    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

在我的创建视图中,我有这个

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

    <div class="form-horizontal">
       <h4>Job</h4>
       <hr />
       @Html.ValidationSummary(true, "", new { @class = "text-danger" })

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

在我的BundleConfig课程中,我实现了

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

bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
        "~/Content/themes/base/jquery.ui.core.css",
        "~/Content/themes/base/jquery.ui.datepicker.css",
        "~/Content/themes/base/jquery.ui.theme.css"));

并在_Layout.cshtml中实现了

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

2 个答案:

答案 0 :(得分:0)

您需要添加一些纯JavaScript代码,例如:http://jqueryui.com/datepicker/(点击查看源代码)。

<script>
$(function() {
  $( "#datepicker" ).datepicker();
});
</script>

而不是“#datepicker”您需要使用日期编辑的ID或“.form-control”来进行具有此类名称的所有编辑

答案 1 :(得分:0)

我的整个_Layout课程,这是对的,对吧?

<!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")
<meta name="description" content="The description of my page" />
</head>
<body>
<div 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="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Substi", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "ChooseTeacher", "Substitutes")</li>
                <li>@Html.ActionLink("All Schools", "Index", "Schools")</li>
                <li>@Html.ActionLink("All Teachers", "Index", "Teachers")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
            </ul>
            @Html.Partial("_LoginPartial")
        </div>
    </div>
</div>
<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/jqueryui")
@RenderSection("scripts", required: false)
</body>
</html>