public ActionResult CreateOrUpdate(NewsModel model, HttpPostedFileBase file) { if (String.IsNullOrEmpty(model.SeName)) model.SeName = model.Title.ToSeName(); var entity = model.Id > 0 ? _newsService.GetById(model.Id) : new News(); Mapper.CreateMap<NewsModel, News>(); Mapper.Map(model, entity); entity.LastUpdated = DateTime.Now; if (file != null && file.ContentLength > 0) { FileHelper _fileHelper = new FileHelper(DIR_NAME, entity.Id > 0 ? entity.CreatedDate : DateTime.Now); entity.ImageUrl = _fileHelper.SaveFile(file); } string msg = string.Empty; if (entity.Id == 0) { entity.CreatedBy = SessionContext.getUser().Email; _newsService.Insert(entity); msg = "Thêm trang nội dung thành công !"; } else { entity.ModifiedBy = SessionContext.getUser().Email; _newsService.Update(entity); msg = "Cập nhật trang nội dung thành công !"; } TempData["Message"] = null; if (!String.IsNullOrEmpty(msg)) TempData["Message"] = msg; return RedirectToAction("Edit", new { Id = entity.Id }); }
public ActionResult Create() { var model = new NewsModel(); model.CreatedDate = DateTime.Now; model.Published = true; PrepareAllCategoriesModel(model); return View(model); }
public ActionResult Edit(int Id) { var entity = _newsService.GetById(Id); var model = new NewsModel(); if (entity != null) { Mapper.CreateMap<News, NewsModel>(); Mapper.Map(entity, model); } model.FullPathImageUrl = FileHelper.GetImageUrlBackEnd(DIR_NAME, model.ImageUrl, model.CreatedDate); PrepareAllCategoriesModel(model); return View(model); }
public ActionResult List() { var entities = _newsService.Table.Where(x => !x.Deleted).OrderByDescending(x => x.CreatedDate).ToList(); IList<NewsModel> models = new List<NewsModel>(); Mapper.CreateMap<News, NewsModel>(); foreach (var item in entities) { var nm = new NewsModel(); Mapper.Map(item, nm); nm.FullPathImageUrl = FileHelper.GetImageUrlBackEnd(DIR_NAME, nm.ImageUrl, nm.CreatedDate); models.Add(nm); } return View(models); }
private void PrepareAllCategoriesModel(NewsModel model) { if (model == null) throw new ArgumentNullException("model"); //model.AvailableCategories.Add(new SelectListItem //{ // Text = "[None]", // Value = "0" //}); IQueryable<Category> categories = _categoryService.Table.Where(x => x.Published && !x.Deleted); foreach (Category c in categories) { if (model.Id == c.Id) continue; model.AvailableCategories.Add(new SelectListItem { Text = GetName(c), Value = c.Id.ToString(CultureInfo.InvariantCulture) }); } }