如何获取Html.Editorfor的日期选择器

时间:2012-04-20 17:57:04

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

在我的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' 

请帮我找一种方法将datepicker添加到我的页面

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

答案 2 :(得分: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>