缺少" Days"的定义

时间:2014-06-13 04:52:22

标签: asp.net-mvc asp.net-mvc-4 view days

我试图突出显示付费日期过期的行或者有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>
}

1 个答案:

答案 0 :(得分:0)

由于PaidDatenullable 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;