如何在MVC项目的视图上添加日期选择器

时间:2018-06-18 14:53:34

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

我正在学习.NET MVC,我在视图上添加日期选择器时遇到了一些困难。

我想要做的是我想创建名为Person.cs的模型

<!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>
    @using System.Web.Optimization
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</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("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("List", "ListPerson", "Person")</li>
                </ul>
            </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/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

_Layout.cshtml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;

namespace WebApplication1.App_Start
{
    public class BundleConfig
    {

        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        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*"));

            // 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/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }
}

BundleConfig.cs

    @model WebApplication1.Models.Person

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2> 

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

    <div class="form-horizontal">
        <h4>Person</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "ListPerson")
</div>

<link href="~/Content/themes/base/jquery-ui.min.css" rel="stylesheet" />

@section Scripts {
    <script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $(function() {
            $(".datepicker").datepicker(
                {
                    dateFormat: "yy/mm/dd",
                    changeMonth: true,
                    changeYear : true
                });
        });
    </script>
}

我正在创建名为 Create.chstml 的视图,它包含用于创建新用户的视图。

@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control datepicker" } })

有没有最简单的方法表明它必须弹出日期选择器?

<script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
</script>
<p>Date: <input type="text" id="datepicker"></p>

我已经在数据包块上安装了 JQuery.UI.Combined

进行一些研究我找到了这个链接: https://jqueryui.com/datepicker/

WITH taniCte
AS (
    SELECT sl.Adi AS [CityName],
        mt.ICD10Kodu AS [Tanı],
        count(mt.ICD10Kodu) AS sayi,
        RowNum = ROW_NUMBER() OVER (
            ORDER BY sl.Adi
            )
    FROM Muayene.Muayene mm WITH (NOLOCK)
    INNER JOIN Muayene.Tani mt WITH (NOLOCK) ON mm.ID = mt.MuayeneId
    INNER JOIN Ortak.Kurum ok WITH (NOLOCK) ON mm.CreatedKurumKodu = ok.KurumKodu
    INNER JOIN Skrs.Il sl WITH (NOLOCK) ON ok.IlKodu = sl.Kodu
    GROUP BY sl.Adi,
        mt.ICD10Kodu
    ) --order by [CityName], sayi desc // commentewhen its moved inside cte
SELECT [CityName],
    [Tanı],
    sayi,
    RowNum
FROM taniCte
WHERE RowNum <= 20
ORDER BY [CityName],
    sayi DESC

实际上我不知道如何在我的代码上实现。

我也想尝试关注此链接:

https://forums.asp.net/t/2134739.aspx?How+to+add+datepicker+to+EditorFor+field+in+asp+net+mvc+5

从此视频中找到解决方案https://www.youtube.com/watch?v=Yuo2XX5_rYo

1 个答案:

答案 0 :(得分:1)

问题可能是你的jQuery选择器:

$("#datepicker").datepicker();

目前,我们会找到包含id datepicker 的HTML元素。查看HTML代码段,它将生成带有id DateOfBirth 的输入(仔细检查检查元素时的样子)。尝试:

$("#DateOfBirth").datepicker();

更好的选择是在HTML元素中添加一个类datepicker(或类似的),然后将其用作选择器,以便它适用于您将来可能希望使用的所有日期选择器:

<强> 查看

@Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control datepicker" } })

<强> 的Javascript

$(".datepicker").datepicker();

我还会研究选择器如何工作,以便您可以更好地确定如何使用它们,例如,如果它以#为前缀,它将查找具有{{1}的元素属性。如果它以id为前缀,那么它将查找具有您指定的类的所有元素。