protected override ValidationResult IsValid(object value, ValidationContext context)
 {
     
     string branch = value.ToString();
     using (VaccinationContext db = new VaccinationContext())
     {
         if (db.Branches1.FirstOrDefault(x => x.Practice_No == branch) != null)
         {
             return ValidationResult.Success;
         }
     }
     var errorMessage = FormatErrorMessage(context.DisplayName);
     return new ValidationResult(errorMessage);
 }
        public ActionResult Edit([Bind(Include = "Id,UserId,Username,Email,Branch_Practice_No,Status")] UserStatus userStatus)
        {
            db = new VaccinationContext();
            if (ModelState.IsValid)
            {
                
                string prev = "";
                using (var tmpDb = new VaccinationContext())
                {
                    var firstOrDefault = tmpDb.UserStatus.FirstOrDefault(x => x.Username == userStatus.Username);
                    if (firstOrDefault != null)
                    {
                        prev = firstOrDefault.Status;
                    }
                }

                db.Entry(userStatus).State = EntityState.Modified;
                db.SaveChanges();

                using (var appdb = new ApplicationDbContext())
                {
                    var item = appdb.Users.FirstOrDefault(x => x.UserName == userStatus.Username);
                    item.Email = userStatus.Email;

                    appdb.SaveChanges();
                }

                if (userStatus.Status == "Active" && prev == "New")
                    {
                        MailService mail = new MailService("*****@*****.**", "V@@5hY9a");
                        mail.SendMail(userStatus.Email, "Access Granted: Confirmation of Access to Vaccination Manager", "<html><body><h3>Hi " + userStatus.Username +"</h3>" + "<br/><p>This message is to confirm an Approval by Admin for your access request on Vaccination Manager.<br/>You will now be able to log onto the system. For more information you can contact your branch manager or Admin.</p><br/><p>Regards</p><b><p>Vaccination Manager Team</p></b></body></html>");

                    }
                
                return RedirectToAction("Index");
            }
            return View(userStatus);
        }