public ActionResult Create(string id) { ////// var modell = new TrainingCenters_Branch(); var stru = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Talent_Development).Structure_Code; var model = dbcontext.TrainingCenters_Branch.ToList(); var code = ""; if (model.Count() == 0) { modell.Branch_Code = stru + "1"; code = stru + "1"; } else { modell.Branch_Code = stru + (model.LastOrDefault().ID + 1).ToString(); code = stru + (model.LastOrDefault().ID + 1).ToString(); } ViewBag.contact_method = dbcontext.Contact_methods.ToList().Select(m => new { Code = m.Code + "-[" + m.Name + ']', ID = m.ID }); ViewBag.centers = dbcontext.TrainingCenter.ToList().Select(m => new { Code = m.TrainingCenters_Code + "-[" + m.TrainingCenters_Desc + ']', ID = m.ID }); ViewBag.country = dbcontext.Country.ToList().Select(m => new { Code = m.Code + "-[" + m.Name + ']', ID = m.ID }); ViewBag.area = new List <Area>(); ViewBag.state = new List <the_states>(); ViewBag.ter = new List <Territories>(); ViewBag.city = new List <cities>(); ViewBag.postal = new List <cities>(); if (id != null) { var ID = int.Parse(id); var center = dbcontext.TrainingCenter.FirstOrDefault(m => m.ID == ID); var new_model = new TrainingCenters_Branch { Branch_Code = code, TrainingCenters_Code = center.TrainingCenters_Code }; return(View(model)); } ///// return(View(modell)); }
public ActionResult create(FormCollection form, TrainingCenters_Branch model) { try { ViewBag.contact_method = dbcontext.Contact_methods.ToList().Select(m => new { Code = m.Code + "-[" + m.Name + ']', ID = m.ID }); ViewBag.centers = dbcontext.TrainingCenter.ToList().Select(m => new { Code = m.TrainingCenters_Code + "-[" + m.TrainingCenters_Desc + ']', ID = m.ID }); ViewBag.country = dbcontext.Country.ToList().Select(m => new { Code = m.Code + "-[" + m.Name + ']', ID = m.ID }); ViewBag.area = new List <Area>(); ViewBag.state = new List <the_states>(); ViewBag.ter = new List <Territories>(); ViewBag.city = new List <cities>(); ViewBag.postal = new List <cities>(); if (model.CountryCode != null && int.Parse(model.CountryCode) > 0) { var ID = int.Parse(model.CountryCode); var country = dbcontext.Country.FirstOrDefault(m => m.ID == ID); ViewBag.area = dbcontext.Area.Where(m => m.Countryid == country.ID.ToString()).ToList().Select(m => new { Code = m.Code + "------[" + m.Name + ']', ID = m.ID }); model.CountryCode = country.Code; } if (model.Region_Code != null && int.Parse(model.Region_Code) > 0) { var ID = int.Parse(model.Region_Code); var region = dbcontext.Area.FirstOrDefault(m => m.ID == ID); ViewBag.state = dbcontext.the_states.Where(m => m.Areaid == region.ID.ToString()).ToList().Select(m => new { Code = m.Code + "------[" + m.Name + ']', ID = m.ID }); model.Region_Code = region.Code; } if (model.State_Governorate_Code != null && int.Parse(model.State_Governorate_Code) > 0) { var ID = int.Parse(model.State_Governorate_Code); var state = dbcontext.the_states.FirstOrDefault(m => m.ID == ID); ViewBag.ter = dbcontext.Territories.Where(m => m.the_statesid == state.ID.ToString()).ToList().Select(m => new { Code = m.Code + "------[" + m.Name + ']', ID = m.ID }); model.State_Governorate_Code = state.Code; } if (model.County_Code != null && int.Parse(model.County_Code) > 0) { var id = int.Parse(model.County_Code); var county = dbcontext.Territories.FirstOrDefault(m => m.ID == id); ViewBag.city = dbcontext.cities.Where(m => m.Territoriesid == county.ID.ToString()).ToList().Select(m => new { Code = m.Code + "------[" + m.Name + ']', ID = m.ID }); model.County_Code = county.Code; } if (model.City_Code != null && int.Parse(model.City_Code) > 0) { var id_code = int.Parse(model.City_Code); var city = dbcontext.cities.FirstOrDefault(m => m.ID == id_code); ViewBag.postal = dbcontext.postcode.Where(m => m.citiesid == city.ID.ToString()).ToList().Select(m => new { Code = m.Code + "------[" + m.Name + ']', ID = m.ID }); model.City_Code = city.Code; } if (model.Zip_Postal_Code != null && int.Parse(model.Zip_Postal_Code) > 0) { var ID = int.Parse(model.Zip_Postal_Code); var postal = dbcontext.postcode.FirstOrDefault(m => m.ID == ID); model.Zip_Postal_Code = postal.Code; } if (ModelState.IsValid) { model.Created_By = User.Identity.Name; model.Created_Date = DateTime.Now.Date; var branch = dbcontext.TrainingCenters_Branch.Add(model); dbcontext.SaveChanges(); var contact_id = form["contact_id"].Split(','); var contact_details = form["contact_details1"].Split(','); for (var i = 0; i < contact_id.Length; i++) { if (contact_id[i] != "") { var ID = int.Parse(contact_id[i]); var details = contact_details[i]; var contact = dbcontext.Contact_methods.FirstOrDefault(m => m.ID == ID); var branch_Contact = new TrainingCenters_Branch_Contact { contact_name = contact.Code + "-" + contact.Name, contact_id = ID, Branch_Code = branch.Branch_Code, ContactMethod_Code = contact.Code, Contact_Method_Detail = details, Created_By = User.Identity.Name, Created_Date = DateTime.Now.Date }; dbcontext.TrainingCenters_Branch_Contact.Add(branch_Contact); dbcontext.SaveChanges(); } } //=================================check for alert================================== var get_result_check = HR.Controllers.check.check_alert("training center branshs", HR.Models.user.Action.Create, HR.Models.user.type_field.form); if (get_result_check != null) { var inbox = new Models.user.Alert_inbox { send_from_user_id = User.Identity.Name, send_to_user_id = get_result_check.send_to_ID_user, title = get_result_check.Subject, Subject = get_result_check.Message }; if (get_result_check.until != null) { if (get_result_check.until.Value.Year != 0001) { inbox.until = get_result_check.until; } } ApplicationDbContext dbcontext = new ApplicationDbContext(); dbcontext.Alert_inbox.Add(inbox); dbcontext.SaveChanges(); } //=================================================================================== return(RedirectToAction("Index")); } else { return(View(model)); } } catch (DbUpdateException) { TempData["Message"] = HR.Resource.Basic.thiscodeIsalreadyexists; return(View(model)); } catch (Exception e) { return(View(model)); } }