2017-09-26 88 views
0

我想創建一個表,其中一個字段有默認值,就像這樣:ASP.NET MVC DataAnnotation - NullTextFormat

enter image description here

我創造了我的視圖模型類:

public class ApplicationDriverLicenseVM 
{ 
    [Required] 
    [Display(Name = "CDL Expiration Date")] 
    [DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "MM/DD/YYYY")] 
    public DateTime? CDLExpDate { get; set; } 

和View(腳手架):

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

,但結果沒有默認的文本,他再次:

enter image description here

爲什麼這麼和我做什麼錯誤?

+2

我相信你可能會尋找一個佔位符。 'new {htmlAttributes = new {@class =「form-control」,placeholder =「MM/DD/YYYY」}' – Nkosi

+1

'NullDisplayText'只有在使用'@ Html.DisplayFor() –

回答

1

NullDisplayText僅顯示在Html.DisplayFor上,而不顯示在Html.EditorFor上。

您可以使用HTML元素的placeholder屬性,並把它添加到EditorForhtmlAttributes對象:

@Html.EditorFor(model => model.CDLExpDate, new { 
     htmlAttributes = new { 
      @class = "form-control", 
      @placeholder = "MM/DD/YYYY" 
     } 
}) 
相關問題