Home > Mobile >  How do I set a default date for a Bootstrap datepicker in Razor?
How do I set a default date for a Bootstrap datepicker in Razor?

Time:10-15

This is probably simple but I can't find anything on it. My code works but I don't know how to set a default date:

<div class="col-md-12 text-center">
    @using (Html.BeginForm())
    {
        <label asp-for="StartDate"></label><br />
        @Html.EditorFor(model => model.StartDate, new { htmlattributes = new { @class = "datepicker" } })
        <br />
        <label asp-for="EndDate"></label><br />
        @Html.EditorFor(model => model.EndDate, new { htmlattributes = new { @class = "datepicker" } })
    }
</div>

Can anyone point me in the right direction? Thanks!

CodePudding user response:

You can try to set default value of Model.StartDate and Model.EndDate.Here is a demo:

Model:

public class TestModel
    {
        public DateTime StartDate { get; set; } = new DateTime(2021, 10, 14, 8, 30, 52);

        public DateTime EndDate { get; set; }= new DateTime(2021, 10, 15, 8, 30, 52);

    }

Action:

public IActionResult Index1()
        {
            return View();
        }

result:

enter image description here

Or you can try to set Model.StartDate and Model.EndDate in action:

Model:

public class TestModel
        {
            public DateTime StartDate { get; set; } 
            public DateTime EndDate { get; set; }
        }

Action:

public IActionResult Index1()
        {
            return View(new RandomModel {  StartDate = new DateTime(2021, 10, 15, 8, 30, 52),EndDate = new DateTime(2021, 10, 16, 8, 30, 52)});
        }

result:

enter image description here

  • Related