Changing the colour of text when expiry date is nearing the end. C# Mvc 3
On my html page I want to change the colour of the date when it close to expiring.
<div class="account-label">Billing Expiry Date</div>
@if (Model.BillingCycleID == 1 && (DateTime.Now.AddDays(23) == 开发者_开发百科true))
{
<div class="account-field2">@Model.BillingEndDate.ToLongDateString()</div>
}
else
{
<div class="account-field">@Model.BillingEndDate.ToLongDateString()</div>
}
@if (Model.BillingCycleID == 2 && (DateTime.Now.AddDays(176) == true))
{
<div class="account-field2">@Model.BillingEndDate.ToLongDateString()</div>
}
else
{
<div class="account-field">@Model.BillingEndDate.ToLongDateString()</div>
}
@if (Model.BillingCycleID == 3 && (DateTime.Now.AddDays(328) == true))
{
<div class="account-field2">@Model.BillingEndDate.ToLongDateString()</div>
}
else
{
<div class="account-field">@Model.BillingEndDate.ToLongDateString()</div>
}
Is there a better way of doing this? I have a billing end date and a billing start date. Billing cycle is for the certain billing cycle they wish to have be it monthly, 6 month or yearly. Account - field 2 changes it to red and account 1 stays white. Thanks :)
I would suggest adding a method to your Model
that defines whether the Model
is close to expiring. This will simplify your view and encapsulate the business logic in the Model
, maintaining the MVC separation of concerns. Something like:
@if (Model.IsCloseToExpiring())
{
<div class="account-field2">@Model.BillingEndDate.ToLongDateString()</div>
}
else
{
<div class="account-field">@Model.BillingEndDate.ToLongDateString()</div>
}
Other things to consider:
Avoid magic numbers, define the number of days until an account becomes about to expire as
const
. It'll make the code easier to understand and easier to modify in the future, if for example, you want to keep these values in configuration or a database.Model.BillingCycleID
isn't very descriptive, would anenum
fit you design instead?
e.g.
public enum BillingCycle
{
Monthly,
Biannually,
Annually
}
精彩评论