【问题标题】:How do I set a default date for a Bootstrap datepicker in Razor?如何在 Razor 中为 Bootstrap 日期选择器设置默认日期?
【发布时间】:2021-10-13 14:01:37
【问题描述】:

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

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

谁能指出我正确的方向?谢谢!

【问题讨论】:

    标签: asp.net-core razor bootstrap-4


    【解决方案1】:

    您可以尝试设置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)});
            }
    

    结果:

    【讨论】:

    • 非常有帮助。我很感激你的时间。这是为了我的调试,所以我不必每次都设置它。
    猜你喜欢
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2019-05-19
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多