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.BillingCycleIDisn't very descriptive, would an- enumfit you design instead?
e.g.
public enum BillingCycle
{
    Monthly,
    Biannually,
    Annually
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论