3

im trying to highlight the rows where the paid date is expired or it has 30 days to be expired. when i build this it says it ... does not contain Definition for the ".Days".

im new to this please help

@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>
}
anfield8
  • 33
  • 5

1 Answers1

4

As PaidDate is nullable DateTime i.e DateTime?, it will return Nullable<TimeSpan> object which does not contain definition of Days so thats why you are getting this error, you need to check if it is not null then cast it to DateTime:

if(item.MembershipType.PaidDate.HasValue)
{
DateTime PaidDate = (DateTime)item.MembershipType.PaidDate;
int daysLeft = (PaidDate - DateTime.Today).Days;
}

or you can cast it in the same line from Nullable<TimeSpan> to TimeSpan:

int daysLeft = ((TimeSpan)(item.MembershipType.PaidDate - DateTime.Today)).Days;
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160