【问题标题】:How to fix that a user should not be able to book a time that has passed如何解决用户无法预订已过时间的问题
【发布时间】:2019-09-21 17:49:12
【问题描述】:

我现在正在处理一个学校项目,我们正在构建一个仅显示今天可用时间的预订系统。 (我们没有使用日历)。我的问题是如何只显示今天的可用时间而不显示过去的时间?现在代码显示从 8.00 到 16.00 的所有时间,即使实际时间是 12.00。如果时钟是 12 点,我只想显示 12 点之后的小时数。希望您能帮助我,因为我还没有找到适合我的解决方案。

这是代码在视图中的样子:

@{
    int open = 8;
    decimal inHours = Convert.ToDecimal(Model.service.Duration) / Convert.ToDecimal(60);
    int iterations = (int)Math.Floor(Convert.ToDecimal(open) / Convert.ToDecimal(inHours));
    DateTime startTime = DateTime.Today;
    startTime = startTime.AddHours(8);

    List<DateTime> dt = new List<DateTime>();
    for (int i = 0; i < iterations; i++) 
    {
        DateTime endTime = startTime;
        endTime = endTime.AddMinutes(Model.service.Duration);
        if (!Model.service.Bookings.Any(x => x.StartTime == startTime)) 
        {
            @Html.ActionLink(startTime.ToString("HH:mm") + "-" + endTime.ToString("HH:mm"),  
                "BookService", "Booking", new 
                    { 
                        inBookingSystemId = Model.bookingSystem.BookingSystemId, 
                        inServiceId = Model.service.ServiceId, 
                        inStartTime = startTime.ToString() 
                     }, new { @class = "btn btn-primary" })
        }
        startTime = endTime;
    }
}

【问题讨论】:

  • 视图中的代码过多。这表明您没有正确准备 ViewModel。在服务器端执行此代码并传递可用小时列表。

标签: c# asp.net entity-framework asp.net-mvc-5 code-first


【解决方案1】:

要仅显示一天中的其他小时,您可以执行以下操作:

List<DateTime> dt = new List<DateTime>();

int hoursToAdd = 1;
int hourNow = DateTime.Now.Hour;

for (int i = hourNow ; i <= 23; i++)
{
    dt.Add(DateTime.Now.AddHours(hoursToAdd));
    hoursToAdd++;
}

这不是最好的方法,可能有更优雅的方法来做,但它会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    相关资源
    最近更新 更多