public void UpdateInstructor(InstructorModel model)
        {
            var instructor = DataContext.Instructors.FirstOrDefault(s => s.InstructorNumber == model.InstructorNumber);
            if (instructor != null)
            {
                instructor.FirstName = model.FirstName;
                instructor.LastName = model.LastName;
                instructor.Gender = model.Gender.ToString();
                instructor.Mobile = model.Mobile;
                instructor.Phone = model.Phone;
                instructor.Status = model.Status;
                instructor.Modified_Date = DateTime.Now;
                instructor.Modified_By = model.CreatedUser;
                //instructor.Areas = model.AreaIds;
                var instructorAddress = DataContext.Addresses.FirstOrDefault(s => s.AddressId == instructor.AddressId);
                if (instructorAddress != null)
                {
                    instructorAddress.Address1 = model.AddressLine1;
                    instructorAddress.AddressLine2 = model.AddressLine2;
                    instructorAddress.Phone = model.Phone;
                    instructorAddress.Mobile = model.Mobile;
                    instructorAddress.PostCode = Convert.ToInt32(model.PostalCode);
                    instructorAddress.ModifiedDate = DateTime.Now;
                    instructorAddress.SuburbID = model.SuburbId;
                    instructorAddress.ModifiedBy = model.CreatedUser;
                }
                var instructorAreas =
                    DataContext.InstructorAreas.Where(v => v.InstructorID == instructor.InstructorId).ToList();
                SaveInDatabase();
                var instructorareas = model.AreaNames.Split(',');
                foreach (var area in instructorAreas)
                {
                    if (instructorareas.FirstOrDefault(s => s == area.AreaId.ToString()) == null)
                    {
                        DataContext.InstructorAreas.Remove(area);
                        DataContext.SaveChanges();
                    }
                }
                foreach (var area in instructorareas)
                {
                    if (instructorAreas.SingleOrDefault(s => s.AreaId == Convert.ToInt32(area)) == null)
                    {
                        DataContext.InstructorAreas.Add(new InstructorArea {AreaId = Convert.ToInt32(area),InstructorID = instructor.InstructorId});
                        SaveInDatabase();
                    }
                }

                
            }
        }
 public ActionResult Edit(InstructorModel model)
 {
     bool status = false;
     if (ModelState.IsValid)
     {
         if (!string.IsNullOrEmpty(model.Password))
         {
             var user = _userManager.FindByEmail(model.Email);
             var provider = Startup.DataProtectionProvider;
             _userManager.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser>(provider.Create("ASP.NET Identity"))
                {
                    TokenLifespan = TimeSpan.FromHours(1)
                };
             string code =  _userManager.GeneratePasswordResetToken(user.Id);
              _userManager.ResetPassword(user.Id, code, model.Password);
         }
         model.CreatedUser = User.Identity.GetUserId();
         string[] addressInfo = model.SuburbName.Split(',');
         var suburb = _postalRepo.GetSuburb(addressInfo[0]);
         model.PostalCode = addressInfo[2];
         if (suburb != null)
         {
             model.SuburbId = suburb.SuburbId;
         }
         _instructorRepo.UpdateInstructor(model);
         status = true;
     }
     model = _instructorRepo.GetInstructorModelByNumber(model.InstructorNumber);
     model.GendersList = new SelectList(_autogearRepo.GenderListItems(), "Value", "Text");
     model.Areas = new SelectList(_instructorRepo.GetAreasList(), "Value", "Text");
     model.SaveStatus = status;
     return View(model);
 }
        public InstructorModel GetInstructorModelByNumber(string instructorNumber)
        {
            var instructor = TblInstructors.FirstOrDefault(s => s.InstructorNumber == instructorNumber);
            var model = new InstructorModel();
            if (instructor != null)
            {
                model.InstructorId = instructor.InstructorId;
                model.FirstName = instructor.FirstName;
                model.LastName = instructor.LastName;
                model.InstructorNumber = instructor.InstructorNumber;
                model.Email = instructor.Email;
                model.Mobile = instructor.Mobile;
                model.Phone = instructor.Phone;
                model.Status = instructor.Status;
                //model.AreaIds = instructor.AreaIds();
                model.AreaNames = instructor.AreaIds;
                if (!string.IsNullOrEmpty(instructor.Gender))
                    model.Gender = Convert.ToInt32(instructor.Gender);
                var instructorAddress = TblAddresses.FirstOrDefault(s => s.AddressId == instructor.AddressId);
                if (instructorAddress != null)
                {
                    model.AddressLine1 = instructorAddress.AddressLine1;
                    model.AddressLine2 = instructorAddress.AddressLine2;
                    model.PostalCode = instructorAddress.PostalCode.ToString();
                    model.SuburbId = instructorAddress.SuburbId;
                }

            }
            return model;
        }