如何在 Razor 中为 Bootstrap 日期选择器设置默认日期?

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

这可能很简单,但我在上面找不到任何内容。我的代码有效,但我不知道如何设置默认日期:

<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>

任何人都可以指出我正确的方向吗?谢谢!

您可以尝试设置Model.StartDateModel.EndDate的默认值。这是一个演示:

型号:

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);

    }

操作:

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

结果:

或者您可以尝试设置 Model.StartDateModel.EndDate

型号:

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

操作:

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

结果: