我遇到了一些问题,试图获得一个列表,可能在用于OrderByDesending
的列中具有空值。我正在处理从客户设施中使用的程序中填充的数据。我正在尝试填充一个约会列表。我不知道为什么系统中有没有日期的约会,除了可能是未来的约会,日期还没有确定。在大多数情况下,你会使用public DateTime? ApptDate { get; set; }
,但然后在我的Foreach视图上,我不能填充日期作为一个字符串,因为它是一个可空值。在进行比较时,我还需要做一些特别的操作,比如
var date = DateTime.Now;
var appd = item.ApptDate ?? date; /// or something like this
int result = DateTime.Compare(appd, date);`
在View中这将不起作用:@item.ApptDate.ToString("MMMM dd yyyy")
我想我要做的是把这个处理为已设置并仍然显示它们的约会。也许用if
else
语句。有人能告诉我,当它确实存在时,该如何表示日期吗?
当前视图:
@foreach (var item in Model)
{
if (item.ApptDate != null)
{
var date = DateTime.Now;
var appd = item.ApptDate;
int result = DateTime.Compare(appd, date);
if (result > 0)
{
<div class="time-label">
<span class="bg-success">
@item.ApptDate.ToString("MMMM dd yyyy")
</span>
</div>
<div>
<i class="fas fa-tooth bg-primary"></i>
<div class="timeline-item">
@{
var time = (item.TimeHr + ":" + item.TimeMin).AsDateTime();
var stdTime = time.ToShortTimeString();
var time2 = time.AddMinutes(item.ApptLength).ToShortTimeString();
}
<h3 class="timeline-header"> @stdTime - @time2 </h3>
<div class="timeline-body">
Appointment Added!
</div>
<div class="timeline-footer">
<a href="#" class="btn btn-primary btn-sm">Confirm</a>
<a href="#" class="btn btn-danger btn-sm">Reschedule</a>
</div>
</div>
</div>
}
}
}
控制器-为了测试,我添加了硬数字:
public ActionResult Appointments()
{
//var user = User.Identity.GetUserId();
//var lookup = db.AppointmentView.Where(x => x.Id == user).FirstOrDefault();
//return View(db.AppointmentView.Where(x => x.Id == user && x.FamilyId == lookup.FamilyId).ToList().OrderByDescending(x => x.ApptDate));
int familyid = 1618;
int patientId = 1451;
return View(db.AppointmentView.Where(x => x.PatientId == patientId && x.FamilyId == familyid && x.ApptDate != null).ToList().OrderByDescending(x => x.ApptDate));
}
谢谢你的帮助。
我明白了。希望对别人有用。这是我的新视图,控制器被更改为加载所有记录与日期或null.
@foreach (var item in Model)
{
var date = DateTime.Now;
var appd = item.ApptDate ?? date;
var dateOf = item.ApptDate != null ? item.ApptDate.Value.ToString("MMMM dd yyyy") : "N/A";
int result = DateTime.Compare(appd, date);
if (item.ApptDate == null)
{
<div class="time-label">
<span class="bg-success">
@dateOf
</span>
</div>
<div>
<i class="fas fa-tooth bg-primary"></i>
<div class="timeline-item">
@{
var time = (item.TimeHr + ":" + item.TimeMin).AsDateTime();
var stdTime = time.ToShortTimeString();
var time2 = time.AddMinutes(item.ApptLength).ToShortTimeString();
}
<h3 class="timeline-header"> @stdTime - @time2 </h3>
<div class="timeline-body">
An appointment has been added but a date is not setup!
</div>
<div class="timeline-footer">
<a href="#" class="btn btn-primary btn-sm">Confirm</a>
<a href="#" class="btn btn-danger btn-sm">Reschedule</a>
</div>
</div>
</div>
}
if (result > 0)
{
<div class="time-label">
<span class="bg-success">
@dateOf
</span>
</div>
<div>
<i class="fas fa-tooth bg-primary"></i>
<div class="timeline-item">
@{
var time = (item.TimeHr + ":" + item.TimeMin).AsDateTime();
var stdTime = time.ToShortTimeString();
var time2 = time.AddMinutes(item.ApptLength).ToShortTimeString();
}
<h3 class="timeline-header"> @stdTime - @time2 </h3>
<div class="timeline-body">
Appointment has been Sheduled!
</div>
<div class="timeline-footer">
<a href="#" class="btn btn-primary btn-sm">Confirm</a>
<a href="#" class="btn btn-danger btn-sm">Reschedule</a>
</div>
</div>
</div>
}
}