public JsonResult GetItem([FromBody] int id) { var msg = new JMessage() { Error = false, Title = "" }; var data = _context.OrderRequestRaws.FirstOrDefault(x => x.Id == id); if (data != null) { var model = new CustomerRequestModel { Id = data.Id, Title = data.Title, Content = data.Content, Phone = data.Phone, Email = data.Email, Priority = data.Priority, RequestTime = data.RequestTime != null?data.RequestTime.Value.ToString("dd/MM/yyyy") : "", Keyword = data.Keyword, url1 = data.File1, FileName1 = data.FileName1, }; msg.Object = model; } else { msg.Error = true; msg.Title = "Không tồn tại dữ liệu"; } return(Json(msg)); }
public JsonResult Insert(CustomerRequestModel obj, IFormFile fileUpload) { var msg = new JMessage() { Error = false, Title = "" }; try { if (fileUpload != null) { var upload = _upload.UploadFile(fileUpload, Path.Combine(_hostingEnvironment.WebRootPath, "uploads\\files")); if (!upload.Error) { var customerRequest = new OrderRequestRaw { Title = obj.Title, Content = obj.Content, Phone = obj.Phone, Keyword = obj.Keyword, Priority = obj.Priority, CreatedTime = DateTime.Now, RequestTime = !string.IsNullOrEmpty(obj.RequestTime) ? DateTime.ParseExact(obj.RequestTime, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null, FileName1 = fileUpload.FileName, File1 = "/uploads/files/" + upload.Object.ToString(), }; _context.OrderRequestRaws.Add(customerRequest); _context.SaveChanges(); msg.Title = "Đã thêm yêu cầu thành công"; } } else { var customerRequest = new OrderRequestRaw { Title = obj.Title, Content = obj.Content, Phone = obj.Phone, Email = obj.Email, Keyword = obj.Keyword, Priority = obj.Priority, CreatedTime = DateTime.Now, RequestTime = !string.IsNullOrEmpty(obj.RequestTime) ? DateTime.ParseExact(obj.RequestTime, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null, }; _context.OrderRequestRaws.Add(customerRequest); _context.SaveChanges(); msg.Title = "Đã thêm yêu cầu thành công"; } } catch (Exception ex) { msg.Error = true; msg.Title = "Thêm yêu cầu lỗi"; } return(Json(msg)); }
public JsonResult Update(CustomerRequestModel obj, IFormFile fileUpload) { var msg = new JMessage() { Error = false, Title = "" }; try { var data = _context.OrderRequestRaws.FirstOrDefault(x => x.Id == obj.Id); if (fileUpload != null) { var upload = _upload.UploadFile(fileUpload, Path.Combine(_hostingEnvironment.WebRootPath, "uploads\\files")); if (!upload.Error) { data.Title = obj.Title; data.Content = obj.Content; data.Phone = obj.Phone; data.Email = obj.Email; data.Keyword = obj.Keyword; data.Priority = obj.Priority; data.RequestTime = !string.IsNullOrEmpty(obj.RequestTime) ? DateTime.ParseExact(obj.RequestTime, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null; data.FileName1 = fileUpload.FileName; data.File1 = "/uploads/files/" + upload.Object.ToString(); _context.OrderRequestRaws.Update(data); _context.SaveChanges(); msg.Title = "Cập nhật yêu cầu thành công"; } } else { data.Title = obj.Title; data.Content = obj.Content; data.Phone = obj.Phone; data.Email = obj.Email; data.Keyword = obj.Keyword; data.Priority = obj.Priority; data.RequestTime = !string.IsNullOrEmpty(obj.RequestTime) ? DateTime.ParseExact(obj.RequestTime, "dd/MM/yyyy", CultureInfo.InvariantCulture) : (DateTime?)null; _context.OrderRequestRaws.Update(data); _context.OrderRequestRaws.Update(data); _context.SaveChanges(); msg.Title = "Cập nhật yêu cầu thành công"; } } catch (Exception ex) { msg.Error = true; msg.Title = "Cập nhật yêu cầu lỗi"; } return(Json(msg)); }