public ActionResult Create([Bind(Include = "AccidentId,AccidentEventLocation,AccidentDescrption,AccidentTime,AccidentDate,AccidentReportUrl,AccidentWid,AccidentTypeId,AccidentZone,AccidentUserId,AccidentUsageId,AccidentTimeStartOperation,AccidentTimeEndOperation,AccidentTimeToClear,AccidentReporter,AccidentReportReciver,AccidentReportType,AccidentSiteFloors,AccidentBuildingType,AccidentBuildingOwner,AccidentBuildingTel,AccidentBuildingTenant,AccidentOtherType,AccidentPreliminaryMeasures,AccidentDescriptionOperation,AccidentDamageDescriptionO,AccidentDamageDescriptionL,AccidentReportProducer,AccidentOperationsCommander,DateAdd,AccidentOperationProblems,AccidentCause")] tbl_Accident tbl_Accident, HttpPostedFileBase[] pic, List <int> OperatingStation, List <int> Employee, List <int> Organisation, string[] InjuredName, string[] InjuredLastName, int[] InjuredSex, int[] InjuredType, int[] InjuredTypeinjury, string[] InjuredDescription, string[] InjuredLocation, int[] MaterialId, int[] tedad) { try { Random rand = new Random(); int te, dateint; if (ModelState.IsValid) { if (Session["OnlineUser"] != null) { if (Session["UserRole"].Equals("SUPERADMIN") || Session["UserRole"].Equals("ADMIN") || Session["UserRole"].Equals("SUBADMIN")) { System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar(); dateint = Convert.ToInt32(pc.GetYear(DateTime.Now)); int accident = db.tbl_Accident.Where(x => x.AccidentId.ToString().Substring(0, 4).Equals(dateint.ToString())).Count(); te = Convert.ToInt32(string.Format("{0}{1}", dateint, accident + 1)); while (db.tbl_Accident.FirstOrDefault(f => f.AccidentId == te) != null) { te = Convert.ToInt32(string.Format("{0}{1}", dateint, accident + 1)); } if (pic != null) { foreach (var item in pic) { var Fi1 = Path.GetExtension(item.FileName); var Ri1 = Path.Combine(Server.MapPath("~/Documents/Doc/"), string.Format("{0}{1}", te.ToString(), Fi1)); item.SaveAs(Ri1); tbl_Accident.AccidentReportUrl = string.Format("Documents/Doc/{0}{1}", te.ToString(), Fi1); } } tbl_Accident.AccidentId = te; tbl_Accident.AccidentUserId = Convert.ToInt32(Session["OnlineUser"].ToString()); tbl_Accident.DateAdd = DateTime.Now; tbl_Accident.Isdelete = false; db.tbl_Accident.Add(tbl_Accident); db.SaveChanges(); int rs, re, rf; foreach (int item in OperatingStation) { tbl_AccidentStation oState = new tbl_AccidentStation(); rs = rand.Next(111111, 999999); while (db.tbl_AccidentStation.FirstOrDefault(f => f.AccidentStationId == rs) != null) { rs = rand.Next(111111, 999999); } oState.AccidentStationId = rs; oState.StationId = item; oState.AccidentId = te; db.tbl_AccidentStation.Add(oState); db.SaveChanges(); } foreach (int item in Employee) { tbl_AccidentEmplyoee oEmployee = new tbl_AccidentEmplyoee(); re = rand.Next(111111, 999999); while (db.tbl_AccidentEmplyoee.FirstOrDefault(f => f.AEId == re) != null) { re = rand.Next(111111, 999999); } oEmployee.AEId = re; oEmployee.EmployeeId = item; oEmployee.AccidentId = te; db.tbl_AccidentEmplyoee.Add(oEmployee); db.SaveChanges(); } foreach (int item in Organisation) { tbl_AccidentO dll = new tbl_AccidentO(); rf = rand.Next(111111, 999999); while (db.tbl_AccidentO.FirstOrDefault(f => f.AccidentOid == rf) != null) { rf = rand.Next(111111, 999999); } dll.AccidentOid = rf; dll.OrganizationsId = item; dll.AccidentId = te; db.tbl_AccidentO.Add(dll); db.SaveChanges(); } for (int i = 0; i < InjuredName.Count(); i++) { tbl_Injured tbl_Injured = new tbl_Injured(); Random rand2 = new Random(); int random = 0; random = rand.Next(11111111, 99999999); while (db.tbl_Injured.FirstOrDefault(x => x.InjuredID == random) != null) { random = rand.Next(11111111, 99999999); } tbl_Injured.InjuredID = random; tbl_Injured.InjuredName = InjuredName[i]; tbl_Injured.InjuredLastName = InjuredLastName[i]; tbl_Injured.InjuredDescription = InjuredDescription[i]; tbl_Injured.InjuredLocation = InjuredLocation[i]; //sex if (InjuredSex[i] == 1) { tbl_Injured.InjuredSex = true; } else if (InjuredSex[i] == 0) { tbl_Injured.InjuredSex = false; } //type if (InjuredType[i] == 1) { tbl_Injured.InjuredType = true; } else if (InjuredType[i] == 0) { tbl_Injured.InjuredType = false; } //masdoumiat if (InjuredTypeinjury[i] == 1) { tbl_Injured.InjuredTypeinjury = true; } else if (InjuredTypeinjury[i] == 0) { tbl_Injured.InjuredTypeinjury = false; } db.tbl_Injured.Add(tbl_Injured); tbl_AccidentInjured tbl_AccidentInjured = new tbl_AccidentInjured(); Random rand1 = new Random(); int random1 = 0; random1 = rand.Next(11111111, 99999999); while (db.tbl_AccidentInjured.FirstOrDefault(x => x.AccidentInjuredid == random1) != null) { random1 = rand.Next(11111111, 99999999); } tbl_AccidentInjured.AccidentInjuredid = random1; tbl_AccidentInjured.AccidentId = te; tbl_AccidentInjured.InjuredId = random; db.tbl_AccidentInjured.Add(tbl_AccidentInjured); db.SaveChanges(); } if (MaterialId != null) { for (int i = 0; i < MaterialId.Count(); i++) { tbl_AccidentM tbl_AccidentM = new tbl_AccidentM(); Random rand1 = new Random(); int random1 = 0; random1 = rand.Next(11111111, 99999999); while (db.tbl_AccidentM.FirstOrDefault(x => x.AccidentMid == random1) != null) { random1 = rand.Next(11111111, 99999999); } tbl_AccidentM.AccidentMid = random1; tbl_AccidentM.AccidentId = te; tbl_AccidentM.MaterialId = MaterialId[i]; tbl_AccidentM.tedad = tedad[i]; db.tbl_AccidentM.Add(tbl_AccidentM); db.SaveChanges(); } return(RedirectToAction("Index")); } else { return(RedirectToAction("Index")); } } else { return(RedirectToAction("Accessdenied", "Home")); } } else { return(RedirectToAction("Login", "Account")); } } return(View(tbl_Accident)); } catch (Exception ex) { ModelState.AddModelError(ex.Message, ex.InnerException.ToString()); return(View()); } }
public ActionResult Create([Bind(Include = "AccidentId,AccidentEventLocation,AccidentDescrption,AccidentTime,AccidentDate,AccidentReportUrl,AccidentWid,AccidentTypeId,AccidentZone,AccidentUserId,AccidentUsageId,AccidentTimeStartOperation,AccidentTimeEndOperation,AccidentTimeToClear,AccidentReporter,AccidentReportReciver,AccidentReportType,AccidentSiteFloors,AccidentBuildingType,AccidentBuildingOwner,AccidentBuildingTel,AccidentBuildingTenant,AccidentOtherType,AccidentPreliminaryMeasures,AccidentDescriptionOperation,AccidentDamageDescriptionO,AccidentDamageDescriptionL,AccidentReportProducer,AccidentOperationsCommander,DateAdd,AccidentOperationProblems,AccidentCause")] tbl_Accident tbl_Accident, HttpPostedFileBase pic, List <int> OperatingStation, List <int> Employee, List <int> Organisation, int injured, int materialnumber) { Random rand = new Random(); int ra, te, dateint; if (ModelState.IsValid) { if (Session["OnlineUser"] != null) { if (Session["UserRole"].Equals("SUPERADMIN") || Session["UserRole"].Equals("ADMIN") || Session["UserRole"].Equals("SUBADMIN")) { System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar(); dateint = Convert.ToInt32(pc.GetYear(DateTime.Now)); te = db.tbl_Accident.Where(x => x.AccidentId.Equals(dateint)).Count(); if (te == 0) { te++; } ra = Convert.ToInt32(string.Format("{0}{1}", dateint, te)); while (db.tbl_Accident.FirstOrDefault(f => f.AccidentId == ra) != null) { ra++; } if (pic != null) { var Fi1 = Path.GetExtension(pic.FileName); var Ri1 = Path.Combine(Server.MapPath("~/Documents/Doc/"), string.Format("{0}{1}", ra.ToString(), Fi1)); pic.SaveAs(Ri1); tbl_Accident.AccidentReportUrl = string.Format("Documents/Doc/{0}{1}", ra.ToString(), Fi1); } tbl_Accident.AccidentId = ra; tbl_Accident.AccidentUserId = Convert.ToInt32(Session["OnlineUser"].ToString()); tbl_Accident.DateAdd = DateTime.Now; tbl_Accident.Isdelete = false; ViewBag.OnlineUser = Session["UserName"].ToString(); ViewBag.OnlineUserRole = Session["UserRole"].ToString(); db.tbl_Accident.Add(tbl_Accident); db.SaveChanges(); int rs, re, rf; foreach (int item in OperatingStation) { tbl_AccidentStation oState = new tbl_AccidentStation(); rs = rand.Next(111111, 999999); while (db.tbl_AccidentStation.FirstOrDefault(f => f.AccidentStationId == rs) != null) { rs = rand.Next(111111, 999999); } oState.AccidentStationId = rs; oState.StationId = item; oState.AccidentId = ra; db.tbl_AccidentStation.Add(oState); db.SaveChanges(); } foreach (int item in Employee) { tbl_AccidentEmplyoee oEmployee = new tbl_AccidentEmplyoee(); re = rand.Next(111111, 999999); while (db.tbl_AccidentEmplyoee.FirstOrDefault(f => f.AEId == re) != null) { re = rand.Next(111111, 999999); } oEmployee.AEId = re; oEmployee.EmployeeId = item; oEmployee.AccidentId = ra; db.tbl_AccidentEmplyoee.Add(oEmployee); db.SaveChanges(); } foreach (int item in Organisation) { tbl_AccidentO dll = new tbl_AccidentO(); rf = rand.Next(111111, 999999); while (db.tbl_AccidentO.FirstOrDefault(f => f.AccidentOid == rf) != null) { rf = rand.Next(111111, 999999); } dll.AccidentOid = rf; dll.OrganizationsId = item; dll.AccidentId = ra; db.tbl_AccidentO.Add(dll); db.SaveChanges(); } if (injured > 0) { return(RedirectToAction("Addinjured", new { id = injured * 9763, Acid = ra * 25438, mat = materialnumber })); } else if (materialnumber > 0) { return(RedirectToAction("Addmaterial", new { id = materialnumber * 3220, Acid = ra * 3221 })); } else { return(RedirectToAction("Index")); } } else { return(RedirectToAction("Accessdenied", "Home")); } } else { return(RedirectToAction("Login", "Account")); } } ViewBag.AccidentTypeId = db.tbl_AccidentType.ToList(); ViewBag.AccidentUsageId = db.tbl_Usage.ToList(); ViewBag.AccidentWid = db.tbl_weather.ToList(); ViewBag.AccidentUserId = db.tbl_User.ToList(); ViewBag.OpState = db.tbl_State.ToList(); ViewBag.Organization = db.tbl_Organizations.ToList(); ViewBag.Emp = db.tbl_Employee.ToList(); ViewBag.material = db.tbl_Material.ToList(); ViewBag.Cause = db.tbl_Cause.ToList(); return(View(tbl_Accident)); }