我试图突出显示付费日期过期的行或者有30天过期的行。
当我构建它时它会说... does not contain Definition for the ".Days"
。
我是新手,请帮忙
@foreach (var item in Model)
{
int daysLeft = (item.MembershipType.PaidDate - DateTime.Today).Days;
string style = daysLeft <= 30 ? "background-color:Red" : null;
<tr style="@style">
<td>
@Html.DisplayFor(modelItem => item.SupplierName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MembershipType.PaidDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
答案 0 :(得分:0)
由于PaidDate
为nullable DateTime
,即DateTime?
,它将返回Nullable<TimeSpan>
对象,该对象不包含Days
的定义,因此这就是您收到此错误的原因,您需要检查它是否为空,然后将其强制转换为DateTime
:
if(item.MembershipType.PaidDate.HasValue)
{
DateTime PaidDate = (DateTime)item.MembershipType.PaidDate;
int daysLeft = (PaidDate - DateTime.Today).Days;
}
或者您可以将它从Nullable<TimeSpan>
转换为TimeSpan
:
int daysLeft = ((TimeSpan)(item.MembershipType.PaidDate - DateTime.Today)).Days;