如何防止用户在同一日期预订约会&;用c#表示的时间?
我可以在if语句中添加什么来比较时间,这样一旦日期相同,它就可以继续检查时间,并让用户知道时间是否已预订如何防止用户在同一日期预订约会&;用c#表示的时间?,c#,sql,asp.net,asp.net-mvc,visual-studio,C#,Sql,Asp.net,Asp.net Mvc,Visual Studio,我可以在if语句中添加什么来比较时间,这样一旦日期相同,它就可以继续检查时间,并让用户知道时间是否已预订 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Appointment appointment) { try { string UserName = User.Identity.Name; appointment.username = UserName;
[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());
}
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果创建(预约)
{
尝试
{
字符串UserName=User.Identity.Name;
appointment.username=用户名;
DateTime date1=约会.AppointmentDateTime.Date;
IEnumerable ApntDate=db.Appointment.ToList(),其中(q=>q.AppointmentDateTime.Date==date1);
如果(ApntDate!=null)
{
DateTime time=System.DateTime.Now;
对于(int i=0;i
您应该使用first或default
如下
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!");}
您应该使用
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!");}
一个观察结果-
ApntDate
永远不会是null
。相反,检查它是否包含元素,例如ApntDate.Any()
。在发布代码之前,您应该格式化代码。“真的很难理解这里发生了什么。”迈克尔会这么做的thanks@GrantWinney干杯,伙计,我现在要做一个观察-ApntDate
永远不会是null
。相反,检查它是否包含元素,例如ApntDate.Any()
。在发布代码之前,您应该格式化代码。“真的很难理解这里发生了什么。”迈克尔会这么做的thanks@GrantWinney干杯,伙计,我现在就去