2017-06-17 77 views
0

我在視圖模型屬性:DataAnnotation和值默認爲

[Display(Name = "Date")] 
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
    public System.DateTime DateTime { get; set; } 

和看法:

<div class="form-group"> 
     @Html.LabelFor(model => model.DateTime, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.DateTime, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

和頁面顯示的默認值01/01/0001在形式:

enter image description here

我想沒有任何默認值(用戶應該指定正確的值,不能發送fo沒有它)。如何刪除默認值?

+1

使屬性'DateTime?'(可爲空),添加'[Required]'屬性並將格式更改爲'DataFormatString =「{0:yyyy-MM-dd}」(HTML-5瀏覽器需要ISO格式,然後以瀏覽器格式顯示) –

回答

1

您必須公開System.DateTime DateTime { get; set; } a 可以爲空DateTime