public ActionResult CreateOrUpdate(CategoryModel model, HttpPostedFileBase file) { if (String.IsNullOrEmpty(model.SeName)) model.SeName = StringUtils.ToSeName(model.Name); var entity = model.Id > 0 ? _categoryService.GetById(model.Id) : new Category(); Mapper.CreateMap<CategoryModel, Category>(); 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) { _categoryService.Insert(entity); msg = "Thêm danh mục thành công !"; } else { _categoryService.Update(entity); msg = "Cập nhật danh mục 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 CategoryModel(); model.CreatedDate = DateTime.Now; model.Published = true; PrepareAllCategoriesModel(model); return View(model); }
public ActionResult CategoryList() { var entities = _categoryService.Table.OrderByDescending(x => x.CreatedDate).ToList(); IList<CategoryModel> models = new List<CategoryModel>(); Mapper.CreateMap<Category, CategoryModel>(); foreach (var item in entities) { CategoryModel cm = new CategoryModel(); Mapper.Map(item, cm); cm.Name = GetCategoryName(item); models.Add(cm); } return PartialView("_CategoryList", models); }
public ActionResult Edit(int Id) { var entity = _categoryService.GetById(Id); var model = new CategoryModel(); if (entity != null) { Mapper.CreateMap<Category, CategoryModel>(); Mapper.Map(entity, model); } PrepareAllCategoriesModel(model); if (!String.IsNullOrEmpty(model.ImageUrl)) { model.FullPathImageUrl = FileHelper.GetImageUrlBackEnd(DIR_NAME, model.ImageUrl, model.CreatedDate); } return View(model); }
private void PrepareAllCategoriesModel(CategoryModel 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) }); } }