如何仅在编辑模式下添加日期? TextBoxFor()

时间:2016-03-23 08:49:04

标签: jquery asp.net-mvc-4 entity-framework-5

enter image description here我想在编辑模式下单击编辑按钮时显示数据库中的日期,但是对于The value '17/03/2016' is not valid for EndDate.等某些日期它不起作用 我想将它添加到数据库,因为它在编辑模式下显示。使用EF5.0.0.0 enter image description here

jquery
    <script type="text/javascript">
                $(function () {
                    $('.datepicker').datepicker({
                        dateFormat: "dd/M/yy",
                        changeMonth: true,
                        changeYear: true,
                        yearRange: "-60:+5"
                    });
                });
            </script>

  @Html.TextBoxFor(model => model.StartDate,"{0:dd/M/yy}", new { @class = "datepicker"})

2 个答案:

答案 0 :(得分:1)

您执行"Mar."但设置"03"

更改

dateFormat: "dd/M/yy",

dateFormat: "dd/mm/yy",

请参阅 jquery-ui Datepicker Formats

最终决议的更新:

将验证代码更改为与mysql-database中的表字段相同的格式修复了该问题。 [DataType(DataType.Date)][DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public Nullable<System.DateTime> CreatedDate { get; set; } @Html.TextBoxFor(model => model.CreatedDate,"{0:yyyy-MM-dd}", new { @class = "datepicker"})

答案 1 :(得分:0)

<script type="text/javascript"> $(function () { $('.datepicker').datepicker({ dateFormat: "yy-MM-dd", changeMonth: true, changeYear: true, yearRange: "-60:+5" }); });</script> 




    [DataType(DataType.Date)][DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
public Nullable<System.DateTime> CreatedDate { get; set; } 





   @Html.TextBoxFor(model => model.CreatedDate,"{0:yyyy-MM-dd}", new { @class = "datepicker"})

对DatePickers的良好推荐..    https://jqueryui.com/datepicker/

enter link description here