Disable Editor Template Date.cshtml on specific view

631 views Asked by At

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.

2

There are 2 answers

3
Jishnu A P On BEST ANSWER

You should use Model instead of DateTime.Now

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

I got alternative solution for this situation

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

Thanks For Help.