Home > other >  ASP.NET Core Changing The Status in my properties model to warning, expired or Current by calculatin
ASP.NET Core Changing The Status in my properties model to warning, expired or Current by calculatin

Time:12-31

In ASP.NET Core, I have properties that are calculated based on other properties, for example, I have

public class Training {

    public int TrainingID { get; set; }
    [Required]
    [Display(Name ="Course Code")]
    public string CourseID { get; set; }

    [Required]
    [Display(Name ="Employee Id")]
    public int EmployeeID { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Initial Date")]
    public DateTime InitialDate { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = " Refrasher Date")]
    public DateTime RefrasherDate { get; set; }

    [Required]
    [Display(Name = "InitialCertificate")]
    public string InitialCertificate { get; set; }

    [Required]
    [Display(Name = "Refrasher Certificate")]
    public string RefrasherCertificate { get; set; }

    [Required]
    [Display(Name = "Updated By")]
    public int UpdatedBy { get; set; }

    
    [Display(Name = "Training Status")]
    public string TrainingStatus { get; set; }


}

and TrainingStatus is meant to be "Current " if (currentdate - RefrasherDate > 90days) would be "Warning " if <=90 and >30 and "Expired" if <=30. the currentdate is not stored in the database but its todays date and TrainingStatus changes dynamically based on the calculated date.

CodePudding user response:

One way is overriding DisplayStatus'get Like this:

 public string TrainingStatus
    {
        get
        {
            var totoalDays = (DateTime.Now - RefrasherDate).TotalDays;
            if (totoalDays > 90) return "Current";
            if (totoalDays <= 90 && totoalDays > 30) return "Warning";
            if (totoalDays < 30) return "Expired";


            return "";
        }
        set { }
    }

CodePudding user response:

Well, that is your model, basically the class that defines a new custom type that is going to be used as a database table. It should be defined under the Models directory. You need to create some kind of auxiliary controller/class to make such verifications.

Define the Controller

//Assuming Training is in the Models directory
using FolderName.Models;
using FolderName.DatabaseContext;
using System.Collections.Generic;
using System.Linq;

public class TrainingController
{
    public static void ChangeStatus(DatabaseContext myContext)
    {
        //Query your database context to retrieve current information 
        //for Training class as a list
        List<Training> myTraining = myContext.Training.ToList();
        
        //Define TrainingStatus status
        foreach(var training in myTraining)
        {
             if((currentDate - training.RefrasherDate) > 90)
                 training.TrainingStatus = "Current";
     
             if((currentDate - training.RefrasherDate) <= 90)
                 training.TrainingStatus = "Warning";

             if((currentDate - training.RefrasherDate) <= 30)
                 training.TrainingStatus = "Expired";
        }
       
        myContext.SaveChanges();
    }

    //Rest of the code
}
  • Related