public CMS_NewsModels GetDetail(string Id) { try { using (var cxt = new CMS_Context()) { var e = cxt.CMS_News.Where(x => x.Id.Equals(Id)).FirstOrDefault(); if (e != null) { var o = new CMS_NewsModels { Id = e.Id, CreatedBy = e.CreatedBy, CreatedDate = e.CreatedDate, Description = e.Description, IsActive = e.IsActive, UpdatedBy = e.UpdatedBy, UpdatedDate = e.UpdatedDate, Title = e.Title, Short_Description = e.Short_Description, ImageURL = e.ImageURL }; return(o); } } } catch (Exception ex) { } return(null); }
public ActionResult Delete(CMS_NewsModels model) { try { //if (!ModelState.IsValid) //{ // Response.StatusCode = (int)HttpStatusCode.BadRequest; // return PartialView("_Delete", model); //} var msg = ""; var result = _factory.Delete(model.Id, ref msg); if (result) { return(RedirectToAction("Index")); } ModelState.AddModelError("Title", msg); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Delete", model)); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Delete", model)); } }
public CMS_NewsModels GetDetail(string Id) { try { using (var cxt = new CMS_Context()) { var item = cxt.CMS_News.Where(x => x.Id.Equals(Id)).FirstOrDefault(); if (item != null) { var o = new CMS_NewsModels { Id = item.Id, CreatedBy = item.CreatedBy, CreatedDate = item.CreatedDate, Description = item.Description, IsActive = item.IsActive, UpdatedBy = item.UpdatedBy, UpdatedDate = item.UpdatedDate, Title = item.Title, Short_Description = item.Short_Description, ImageURL = item.ImageURL }; /* update empID */ var lstEmp = cxt.CMS_Employees.Where(e => e.Id == o.CreatedBy || e.Id == o.UpdatedBy).ToList(); o.CreatedBy = lstEmp.Where(e => e.Id == o.CreatedBy).Select(e => e.FirstName + " " + e.LastName).FirstOrDefault(); o.UpdatedBy = lstEmp.Where(e => e.Id == o.UpdatedBy).Select(e => e.FirstName + " " + e.LastName).FirstOrDefault(); return(o); } } } catch (Exception ex) { } return(null); }
public bool CreateOrUpdate(CMS_NewsModels model, ref string msg) { var Result = true; using (var cxt = new CMS_Context()) { using (var trans = cxt.Database.BeginTransaction()) { try { if (string.IsNullOrEmpty(model.Id)) { var _Id = Guid.NewGuid().ToString(); var e = new CMS_News { Id = _Id, Title = model.Title, Short_Description = model.Short_Description, ImageURL = model.ImageURL, CreatedBy = model.CreatedBy, CreatedDate = DateTime.Now, Description = model.Description, UpdatedBy = model.UpdatedBy, UpdatedDate = DateTime.Now, IsActive = model.IsActive }; cxt.CMS_News.Add(e); } else { var e = cxt.CMS_News.Find(model.Id); if (e != null) { e.Title = model.Title; e.Short_Description = model.Short_Description; e.ImageURL = model.ImageURL; e.Description = model.Description; e.UpdatedBy = model.UpdatedBy; e.UpdatedDate = DateTime.Now; e.IsActive = model.IsActive; } } cxt.SaveChanges(); trans.Commit(); } catch (Exception) { Result = false; msg = "Vui lòng kiểm tra đường truyền"; trans.Rollback(); } finally { cxt.Dispose(); } } } return(Result); }
public ActionResult Create() { CMS_NewsModels model = new CMS_NewsModels(); model.ListBlogType = GetListBlogType(); model.Type = (byte)Commons.EBlogType.Image; return(PartialView("_Create", model)); }
public ActionResult Create(CMS_NewsModels model) { try { byte[] photoByte = null; if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; model.ListBlogType = GetListBlogType(); return(PartialView("_Create", model)); } if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0) { Byte[] imgByte = new Byte[model.PictureUpload.ContentLength]; model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength); model.PictureByte = imgByte; model.ImageURL = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName); model.PictureUpload = null; photoByte = imgByte; } var msg = ""; model.CreatedBy = CurrentUser.Email; model.UpdatedBy = CurrentUser.Email; var result = _factory.CreateOrUpdate(model, ref msg); if (result) { if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null) { var path = Server.MapPath("~/Uploads/News/" + model.ImageURL); MemoryStream ms = new MemoryStream(photoByte, 0, photoByte.Length); ms.Write(photoByte, 0, photoByte.Length); System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true); ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte, 400, Commons.WidthImageNews, Commons.HeightImageNews); } return(RedirectToAction("Index")); } ModelState.AddModelError("Title", msg); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Create", model)); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Create", model)); } }
public CMS_NewsModels GetDetail(string Id) { NSLog.Logger.Info("NewsGetDetail", Id); CMS_NewsModels ret = null; try { using (var cxt = new CMS_Context()) { var e = cxt.CMS_News.Where(x => x.Id.Equals(Id) && x.Status == (byte)Commons.EStatus.Actived).FirstOrDefault(); if (e != null) { ret = new CMS_NewsModels { Id = e.Id, CreatedBy = e.CreatedUser, CreatedDate = e.CreatedDate, Description = e.Description, IsActive = e.IsActive, Title = e.Title, Short_Description = e.Short_Description, Type = e.Type, Link = e.Link, ImageURL = e.ImageURL, Author = e.Author, Category = e.Category, Source = e.Source, UpdatedBy = e.ModifiedUser, UpdatedDate = e.LastModified, }; } } NSLog.Logger.Info("ResponseNewsGetDetail", ret); } catch (Exception ex) { NSLog.Logger.Info("ErrorNewsGetDetail", ex); } return(ret); }
public ActionResult Create() { CMS_NewsModels model = new CMS_NewsModels(); return(PartialView("_Create", model)); }
public ActionResult Edit(CMS_NewsModels model) { var temp = model.ImageURL; try { byte[] photoByte = null; if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Edit", model)); } if (!string.IsNullOrEmpty(model.ImageURL)) { model.ImageURL = model.ImageURL.Replace(Commons._PublicImages + "News/", "").Replace(Commons.Image272_259, ""); } if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0) { Byte[] imgByte = new Byte[model.PictureUpload.ContentLength]; model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength); model.PictureByte = imgByte; model.ImageURL = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName); model.PictureUpload = null; photoByte = imgByte; } var msg = ""; var result = _factory.CreateOrUpdate(model, ref msg); if (result) { if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null) { if (!string.IsNullOrEmpty(temp)) { temp = temp.Replace(Commons._PublicImages + "News/", "").Replace(Commons.Image272_259, ""); temp = "~/Uploads/News/" + temp; if (System.IO.File.Exists(Server.MapPath(temp))) { ImageHelper.Me.TryDeleteImageUpdated(Server.MapPath(temp)); } } var path = Server.MapPath("~/Uploads/News/" + model.ImageURL); MemoryStream ms = new MemoryStream(photoByte, 0, photoByte.Length); ms.Write(photoByte, 0, photoByte.Length); System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true); ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte, 400, Commons.WidthImageNews, Commons.HeightImageNews); } return(RedirectToAction("Index")); } ModelState.AddModelError("Title", msg); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Edit", model)); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView("_Edit", model)); } }
public bool CreateOrUpdate(CMS_NewsModels model, ref string msg) { NSLog.Logger.Info("NewsCreateOrUpdate", model); var Result = true; using (var cxt = new CMS_Context()) { try { if (string.IsNullOrEmpty(model.Id)) /* insert */ { var _Id = Guid.NewGuid().ToString(); var e = new CMS_News { Id = _Id, Title = model.Title, Link = model.Link, Short_Description = model.Short_Description, Type = model.Type, ImageURL = model.ImageURL, Description = model.Description, IsActive = model.IsActive, Author = model.Author, Category = model.Category, Source = model.Source, CreatedUser = model.CreatedBy, CreatedDate = DateTime.Now, ModifiedUser = model.UpdatedBy, LastModified = DateTime.Now, Status = (byte)Commons.EStatus.Actived, }; cxt.CMS_News.Add(e); } else /* updated */ { var e = cxt.CMS_News.Find(model.Id); if (e != null) { e.Title = model.Title; e.Short_Description = model.Short_Description; e.Type = model.Type; e.Link = model.Link; e.ImageURL = model.ImageURL; e.Description = model.Description; e.Author = model.Author; e.Source = model.Source; e.Category = model.Category; e.ModifiedUser = model.UpdatedBy; e.LastModified = DateTime.Now; e.IsActive = model.IsActive; } } cxt.SaveChanges(); NSLog.Logger.Info("ResponseNewsCreateOrUpdate", new { Result, msg }); } catch (Exception ex) { Result = false; msg = "Vui lòng kiểm tra đường truyền"; NSLog.Logger.Error("ErrorNewsCreateOrUpdate", ex); } } return(Result); }