How to get a datepicker for Html.Editorfor

后端 未结 3 1068
臣服心动
臣服心动 2021-02-08 13:14

On my MVC3 razor page I have a date field

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

Given below is the code I am trying to get a datepi

相关标签:
3条回答
  • 2021-02-08 13:42

    You'll need to create an EditorTemplate in your Views\Shared\EditorTemplate folder. Name it DateTime.cshtml. Should look something like the following:

    @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>
    

    Use it as follows:

    @Html.EditorFor(model => model.Member.Dob)
    
    0 讨论(0)
  • 2021-02-08 13:42

    This article covers this perfectly: http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

    0 讨论(0)
  • 2021-02-08 14:03
    <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>
    
    0 讨论(0)
提交回复
热议问题