Steve Cross
Steve Cross

Reputation: 99

How do I set an initial value for a date in mvc?

So I have researched this for several days and I am trying to put an initial value to a date. It needs to be 4 weeks from yesterday so the model has this in it:

    [DataType(DataType.Date, ErrorMessage = "Date Only")]
    [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
    [Display(Name = "Starting Date")]
    public DateTime StartDate { get; set; } = DateTime.Today.AddDays(-29);

This is the code for the view:

    @Html.EditorFor(model => model.StartDate, new { htmlattributes = new { @class = "datepicker" } })

Yet, whether I have the default date in the model or not, the view yields this:

enter image description here

I want it to be changeable but this is the initial value I am trying to deploy.

I have also tried this in the view. No Change.

    @Html.EditorFor(model => model.StartDate, new { htmlattributes = new { @class = "datepicker", @Value = DateTime.Today.AddDays(-29) } })

Upvotes: 0

Views: 573

Answers (2)

Mulat
Mulat

Reputation: 1

@Html.TextBoxFor(model => model.BirthDate, "{0:yyyy-MM-dd}", new { @class = "form-control", type = "date" })

Upvotes: 0

KHAL
KHAL

Reputation: 337

you can pass it using Razor, Read more

@Html.EditorFor(model => model.StartDate, DateTime.Today.AddDays(-29), new { htmlattributes = new { @class = "datepicker" } })

Upvotes: 1

Related Questions