asp.net-mvcrazorvieweditortemplates

Disable Editor Template Date.cshtml on specific view


My Model

    [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; }

My View

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

My Date.cshtml (Editor Templates)

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

My requirement is show database value on this particular view but every time it shows me current date. How to do this Please help me.


Solution

  • You should use Model instead of DateTime.Now

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