asp.net如何防止用户对相同的日期及时间在 c# 中的预约?

发布时间: 2016/7/10 16:56:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

可以将什么添加到 if 语句来比较时间,所以一旦日期是相同它会移到检查的时间,让用户知道时间是否已经预订?

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Appointment appointment)
{
    try
    {
       string UserName = User.Identity.Name;
       appointment.username = UserName;

       DateTime date1 = appointment.AppointmentDateTime.Date;

       IEnumerable<Appointment> ApntDate = db.Appointment.ToList().Where(q=> q.AppointmentDateTime.Date == date1);                   

       if (ApntDate != null)
       {
            DateTime time = System.DateTime.Now;
            for (int i = 0; i < ApntDate.Count(); i++)
            {
                 if (date1 == ApntDate.ElementAt(i).AppointmentDateTime)
                 {

                 }
            }
        }

        db.Appointment.Add(appointment);
        db.SaveChanges();

        return RedirectToAction("Index", "Appointment");       
    }
    catch (Exception ex)
    {
      return View(ex.ToString());
    }
 }

解决方法 1:

您应该使用 FirstOrDefault ,如下所示

   DateTime date1 = appointment.AppointmentDateTime.Date;


   var ApntDate = 
           db.Appointment
           .FirstOrDefault(q=>q.AppointmentDateTime.Date == date1);                   

     if(ApntDate!=null){//book new}
     else{return Content("You have already booked!");}
官方微信
官方QQ群
31647020