public async Task <IActionResult> EditPortfolioItem(EditPortfolioItemViewModel portfoliovm) { var portfolioItem = new PortfolioItem { Id = portfoliovm.Id, Title = portfoliovm.Title, Introduction = portfoliovm.Introduction, Body = portfoliovm.Body, Description = portfoliovm.Description, Tags = portfoliovm.Tags, CommentsAllowed = portfoliovm.CommentsAllowed, SourceCodeLink = portfoliovm.SourceCodeLink, Slug = SlugGenerator.ToSlug(portfoliovm.Title) }; if (portfoliovm.Image == null) { portfolioItem.Image = portfoliovm.CurrentImage; } else { if (!string.IsNullOrEmpty(portfoliovm.CurrentImage)) { _fileManager.RemovePortfolioImage(portfoliovm.CurrentImage); } portfolioItem.Image = _fileManager.SavePortfolioItemImage(portfoliovm.Image); } if (portfolioItem.Id > 0) { if (portfoliovm.CategoryId != portfoliovm.CurrentCategoryId) { var category = _repo.GetCategoryNoTracking(portfoliovm.CategoryId); portfolioItem.Category = category; } portfolioItem.CreatedDate = portfoliovm.CreatedDate; _repo.UpdatePortfolioItem(portfolioItem); } else { var category = _repo.GetCategory(portfoliovm.CategoryId); portfolioItem.Category = category; _repo.AddPortfolioItem(portfolioItem); } if (await _repo.SaveChangesAsync()) { return(RedirectToAction("ManagePortfolio")); } else { return(View(portfolioItem)); } }
public IActionResult EditPortfolioItem(int?id) { var categoryList = _repo.GetAllCategories().ToList(); var dropDownList = new SelectList(categoryList.Select(item => new SelectListItem { Text = item.CategoryName, Value = item.Id.ToString() }).ToList(), "Value", "Text"); if (id == null) { var vm = new EditPortfolioItemViewModel() { CategoryList = dropDownList }; return(View(vm)); } else { var portfolioItem = _repo.GetPortfolioItem((int)id); return(View(new EditPortfolioItemViewModel { Id = portfolioItem.Id, Title = portfolioItem.Title, Slug = portfolioItem.Slug, CreatedDate = portfolioItem.CreatedDate, Introduction = portfolioItem.Introduction, Body = portfolioItem.Body, CurrentImage = portfolioItem.Image, Description = portfolioItem.Description, Tags = portfolioItem.Tags, CurrentCategoryId = portfolioItem.Category.Id, CategoryId = portfolioItem.Category.Id, CategoryList = dropDownList, SourceCodeLink = portfolioItem.SourceCodeLink, CommentsAllowed = portfolioItem.CommentsAllowed }));; } }