1

我的模特

    [DataType(DataType.Date, ErrorMessage = "Please enter a valid date in the format MM/dd/yyyy")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    [Display(Name = "From Date")]
    public DateTime FromDate { get; set; }

我的观点

 @Html.EditorFor(model => model.FromDate)

我的Date.cshtml(编辑器模板)

@Html.TextBox("", string.Format("{0:MM/dd/yyyy}", DateTime.Now),
 new { @class = "datefield", type = "date"  })

我的要求是在这个特定视图上显示数据库值,但每次它都显示当前日期。如何做到这一点请帮助我。

4

2 回答 2

1

你应该使用Model而不是DateTime.Now

@Html.TextBox("", string.Format("{0:MM/dd/yyyy}", Model),
new { @class = "datefield", type = "date"  })
于 2014-10-29T10:06:26.417 回答
0

我有这种情况的替代解决方案

   @if (@Model.FromDate != null)
    {
        var frmDate = Model.FromDate;
        @Html.EditorFor(model => frmDate)
    }

感谢帮助。

于 2014-10-29T10:48:47.187 回答