private ActionResult ViewDetail(TblKpt ob, string msg, AlertMsgType?msgType) { try { if (ob == null) { throw new Exception("ไม่พบข้อมูลที่ต้องการ, กรุณาลองใหม่อีกครั้ง"); } if (!string.IsNullOrWhiteSpace(msg)) { WidgetAlertModel alert = new WidgetAlertModel() { Message = msg }; if (msgType.HasValue) { alert.Type = msgType.Value; } ViewBag.Alert = alert; } ViewData["GoogleMapsAPIKey"] = ConfigurationManager.AppSettings["GoogleMapsAPI"]; return(View("Detail", ob)); } catch (Exception ex) { return(RedirectToAction("Index", new { area = "", controller = "KPT", msg = ex.GetMessage(), msgType = AlertMsgType.Danger })); } }
public ActionResult SetAttach() { int kptId = Request.Form["KptId"].ParseInt(); //SysRole role = CurrentUser.Role(3).Role(); //ViewBag.canWrite = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 2 && x.PermissionFlag).Count() > 0; //ViewBag.canDelete = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 3 && x.PermissionFlag).Count() > 0; //ViewBag.canExport = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 4 && x.PermissionFlag).Count() > 0; TblKpt ob = db.TblKpt.Find(kptId); try { TblKptAttachment attach = new TblKptAttachment() { TblKpt = ob, AttachName = Request.Form["AttachName"].Convert2String() }; if (Request.Files.Count > 0 && Request.Files["AttachFile"] != null && Request.Files["AttachFile"].ContentLength > 0) { HttpPostedFileBase uploadedFile = Request.Files["AttachFile"]; string FilePath = string.Format("files/kpt/{0}{1}", CurrentDate.ParseString(DateFormat._yyyyMMddHHmmssfff), Path.GetExtension(uploadedFile.FileName)); if (!Directory.Exists(Server.MapPath("~/files"))) { Directory.CreateDirectory(Server.MapPath("~/files")); } if (!Directory.Exists(Server.MapPath("~/files/kpt"))) { Directory.CreateDirectory(Server.MapPath("~/files/kpt")); } uploadedFile.SaveAs(Server.MapPath("~/" + FilePath)); attach.AttachPath = FilePath; } else { throw new Exception("กรุณาอัพโหลดไฟล์แนบ"); } db.TblKptAttachment.Add(attach); db.SaveChanges(); return(RedirectToAction("Detail", "KPT", new { id = ob.KptId, msg = "บันทึกข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); //return ViewDetail(ob, "บันทึกข้อมูลเรียบร้อยแล้ว", AlertMsgType.Success); } catch (Exception ex) { string msg = ex.GetMessage(); return(RedirectToAction("Detail", "KPT", new { id = ob.KptId, msg, msgType = AlertMsgType.Danger })); } }
public ActionResult Delete(int id) { try { SysRole role = CurrentUser.Role(3).Role(); bool canDelete = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 3 && x.PermissionFlag).Count() > 0; if (!canDelete) { return(RedirectToAction("Index", "KPT", new { msg = "ไม่มีสิทธิ์ในการลบข้อมูล", msgType = AlertMsgType.Danger })); } TblKpt ob = db.TblKpt.Find(id); if (ob == null) { return(RedirectToAction("Index", "KPT", new { msg = "ไม่พบข้อมูลที่ต้องการ", msgType = AlertMsgType.Warning })); } var subs = db.TblKptDetail.Where(x => x.KptId == id); db.TblKptDetail.RemoveRange(subs); var stations = db.TblKptStation.Where(x => x.KptId == id); var stationAttachs = db.TblKptStationAttachment.Where(x => stations.Select(y => y.StationId).Contains(x.StationId)); List <string> imgsPath = stationAttachs.Select(x => x.AttachPath).ToList(); var stationDetails = db.TblKptStationDetail.Where(x => stations.Select(y => y.StationId).Contains(x.StationId)); db.TblKptStationDetail.RemoveRange(stationDetails); db.TblKptStationAttachment.RemoveRange(stationAttachs); db.TblKptStation.RemoveRange(stations); db.TblKpt.Remove(ob); db.SaveChanges(); if (imgsPath.Count() > 0) { foreach (string path in imgsPath) { string svPath = Server.MapPath("~/" + path); if (FileIO.Exists(svPath)) { FileIO.Delete(svPath); } } } return(RedirectToAction("Index", "KPT", new { msg = "ลบข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); } catch (Exception ex) { return(RedirectToAction("Index", "KPT", new { msg = ex.GetMessage(), msgType = AlertMsgType.Danger })); } }
public ActionResult Detail(int?id, string msg, AlertMsgType?msgType) { SysRole role = CurrentUser.Role(3).Role(); ViewBag.canWrite = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 2 && x.PermissionFlag).Count() > 0; ViewBag.canDelete = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 3 && x.PermissionFlag).Count() > 0; ViewBag.canExport = role.SysRolePermission.Where(x => x.MenuId == 3220 && x.PermissionId == 4 && x.PermissionFlag).Count() > 0; TblKpt ob = uow.db.TblKpt .Include(x => x.TblKptDetail) .Include(x => x.TblKptAttachment) .Where(x => x.KptId == id).FirstOrDefault() ?? new TblKpt() { TblKptDetail = new List <TblKptDetail>(), TblKptAttachment = new List <TblKptAttachment>() }; if (ob.KptId <= 0) { ob.KptDate = CurrentDateTime.Date; } return(ViewDetail(ob, msg, msgType)); }
public ActionResult SetDetail() { int Id = Request.Form["KptId"].ParseInt(); TblKpt ob = uow.db.TblKpt.Find(Id) ?? new TblKpt(); if (ob.KptId <= 0) { ob.KptDate = CurrentDateTime.Date; ob.CreatedBy = CurrentUID; ob.CreatedDate = CurrentDateTime; } ob.ProjectName = Request.Form["ProjectName"]; ob.CustomerName = Request.Form["CustomerName"]; ob.KptLatitude = Request.Form["KptLatitude"]; ob.KptLongtitude = Request.Form["KptLongtitude"]; ob.SubDistrictId = Request.Form["SubDistrictId"].ParseInt(); ob.KptLocation = Request.Form["KptLocation"]; //ob.KptStation = Request.Form["KptStation"]; ob.KptRemark = Request.Form["KptRemark"]; ob.TestBy = Request.Form["TestBy"]; ob.UpdatedBy = CurrentUID; ob.UpdatedDate = CurrentDateTime; try { bool flagInsert = false; if (ob.KptId <= 0) { uow.db.TblKpt.Add(ob); flagInsert = true; } else { uow.db.Entry(ob).State = EntityState.Modified; } uow.SaveChanges(); if (flagInsert && ob.KptId > 0) { return(RedirectToAction("Detail", new { id = ob.KptId, controller = "KPT", msg = "บันทึกข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); } else { return(RedirectToAction("Index", new { area = "", controller = "KPT", msg = "บันทึกข้อมูลเรียบร้อยแล้ว", msgType = AlertMsgType.Success })); } } catch (DbEntityValidationException ex) { string msg = ""; foreach (var eve in ex.EntityValidationErrors) { msg += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { msg += string.Format("{{\n}}- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } return(ViewDetail(ob, msg, AlertMsgType.Danger)); } catch (Exception ex) { string msg = ex.GetMessage(); return(ViewDetail(ob, msg, AlertMsgType.Danger)); } }