11

在我的 MVC3 剃须刀页面上,我有一个日期字段

@Html.EditorFor(model => model.Member.Dob)

下面给出的是我试图为“出生日期”字段获取日期选择器的代码。

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
 <script type="text/javascript">
      $(document).ready(function () {
          debugger;
          $("#Member_Dob").datepicker();
      });
  </script>

但是当我运行这个页面时,我得到了错误

Microsoft JScript runtime error: Object doesn't support property or method 'datepicker' 

请帮我找到一种将日期选择器添加到我的页面的方法

4

3 回答 3

21

您需要在 Views\Shared\EditorTemplate 文件夹中创建一个 EditorTemplate。将其命名为 DateTime.cshtml。应该如下所示:

@model System.DateTime?
 <div class="editor-label">
    @Html.Label("")
</div>
<div class="editor-field">
    @Html.TextBox("", String.Format("{0:MM/dd/yyyy}",(Model == DateTime.MinValue)? null : Model), new { @class="text"})
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#@ViewData.ModelMetadata.PropertyName").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'mm/dd/yy',
            gotoCurrent: true
        });
    });
</script>

按如下方式使用它:

@Html.EditorFor(model => model.Member.Dob)
于 2012-04-20T18:02:24.980 回答
1

这篇文章完美地涵盖了这一点:http: //blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-和-添加-a-jquery-validate-date-range-validator.aspx

于 2013-01-23T08:12:46.443 回答
0
<div class="form-group">
                @Html.LabelFor(model => model.Birthdate, "Birthday", new { @class = "control-label col-md-2" })
                <div class="col-md-5">
                    @Html.Kendo().DatePickerFor(model => model.Birthdate)
                    @Html.ValidationMessageFor(model => model.Birthdate)
                </div>
            </div>
于 2020-07-02T20:48:27.550 回答