public ActionResult Create(string id) { var modell = new Sub_educational_body(); var stru = dbcontext.StructureModels.FirstOrDefault(m => m.All_Models == ChModels.Basic).Structure_Code; var modelll = dbcontext.Sub_educational_body.ToList(); var Code = ""; if (modelll.Count() == 0) { Code = stru + "1"; } else { Code = stru + (modelll.LastOrDefault().ID + 1).ToString(); } ViewBag.main = dbcontext.Main_Educate_body.ToList().Select(m => new { Code = m.Code + "----" + m.Name, ID = m.ID }); if (id != null) { var ID = int.Parse(id); var Main_Educate_body = dbcontext.Main_Educate_body.FirstOrDefault(m => m.ID == ID); var model = new Sub_educational_body { Code = Code, Main_Educate_body = Main_Educate_body, Main_Educate_bodyid = Main_Educate_body.ID }; return(View(model)); } var mmodel = new Sub_educational_body(); mmodel.Code = Code; return(View(mmodel)); }
public ActionResult Create(Sub_educational_body model) { try { ViewBag.main = dbcontext.Main_Educate_body.ToList().Select(m => new { Code = m.Code + "----" + m.Name, ID = m.ID }); if (ModelState.IsValid) { Sub_educational_body record = new Sub_educational_body(); record.Name = model.Name; record.Description = model.Description; record.Main_Educate_bodyid = model.Main_Educate_bodyid; record.Code = model.Code; record.Main_Educate_body = dbcontext.Main_Educate_body.FirstOrDefault(m => m.ID == model.Main_Educate_bodyid); dbcontext.Sub_educational_body.Add(record); dbcontext.SaveChanges(); //=================================check for alert================================== var get_result_check = HR.Controllers.check.check_alert("qulification sub provider", 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"] = "this code Is already exists"; return(View(model)); } catch (Exception e) { return(View(model)); } }