public ActionResult Create(PortfolioItemViewModel portfolioItemViewModel, HttpPostedFileBase Image)
        {
            if (Image != null)
            {
                string ImageName = System.IO.Path.GetFileName(Image.FileName);
                string path = System.IO.Path.Combine(
                                       Server.MapPath("~/Images/PortfolioItems"), ImageName);
                // file is uploaded
                Image.SaveAs(path);

                portfolioItemViewModel.Image = ImageName;
            }

            if (ModelState.IsValid)
            {
                var portfolioItem = new PortfolioItem {
                    Title = portfolioItemViewModel.Title,
                    Description = portfolioItemViewModel.Description,
                    Image = portfolioItemViewModel.Image,
                    ProjcetUrl = portfolioItemViewModel.ProjcetUrl,
                };

                AddOrUpdateTags(portfolioItem, portfolioItemViewModel.Tags);
                db.PortfolioItems.Add(portfolioItem);;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(portfolioItemViewModel);
        }
Пример #2
0
        public static PortfolioItem ToDomainModel(this PortfolioItemViewModel portfolioItemViewModel)
        {
            var portfolioItem = new PortfolioItem();
            portfolioItem.Title = portfolioItemViewModel.Title;
            portfolioItem.ID = portfolioItemViewModel.ID;
            portfolioItem.ProjcetUrl = portfolioItemViewModel.ProjcetUrl;
            portfolioItem.Image = portfolioItemViewModel.Image;

            return portfolioItem;
        }
        private void AddOrUpdateTags(PortfolioItem portfolioItem, IEnumerable<AssignedTags> assignedTags)
        {
            if (assignedTags == null) return;
            if (portfolioItem.ID != 0)
            {
                foreach (var tag in portfolioItem.Tags.ToList())
                {
                    portfolioItem.Tags.Remove(tag);
                }

                foreach (var tag in assignedTags.Where(c => c.Assigned))
                {
                    portfolioItem.Tags.Add(db.Tags.Find(tag.ID));
                }
            }
            else
            {
                // New user
                foreach (var assignedTag in assignedTags.Where(c => c.Assigned))
                {
                    var tag = new Tag { ID = assignedTag.ID };
                    db.Tags.Attach(tag);
                    portfolioItem.Tags.Add(tag);
                }
            }
        }