public ActionResult Create(ProductPropertyManageCreateViewModel model) { if (ModelState.IsValid) { var productProperty = new ProductProperty { Name = model.Name, Description = model.Description, PlusPrice=model.PlusPrice }; db.ProductProperties.Add(productProperty); db.SaveChanges(); if (model.PropertyImg != null) { var uploadedFile = new UploadedFile(model.PropertyImg); var propertyImgName = uploadedFile.SaveAs(Server.MapPath("~/ImgRepository/ProductPropertyImgs/")); var pathRel = Url.Content("~/ImgRepository/ProductPropertyImgs/" + propertyImgName); productProperty.ImgUrl = pathRel; db.Entry(productProperty).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } return View(model); }
public ActionResult Edit(ProductPropertyManageEditViewModel model) { if (ModelState.IsValid) { string newPropertymgUrl; if (model.PropertyImg != null) { var uploadedFile = new UploadedFile(model.PropertyImg); var propertyImgName = uploadedFile.SaveAs(Server.MapPath("~/ImgRepository/ProductPropertyImgs/")); var pathRel = Url.Content("~/ImgRepository/ProductPropertyImgs/" + propertyImgName); newPropertymgUrl = pathRel; } else { newPropertymgUrl = db.Products.Find(model.Id).CoverImgUrl; } var productProperty = new ProductProperty { Id=model.Id, Name=model.Name, Description=model.Description, ImgUrl= newPropertymgUrl, PlusPrice=model.PlusPrice }; db.Entry(productProperty).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(model); }
public ActionResult Create(ProductManageCreateViewModel model) { if (ModelState.IsValid) { //find properties in db var productPropertis = db.ProductProperties.Where(p => model.ProductPropertyForProduct.Contains(p.Id)).ToList(); var product = new Product { Name = model.Name, ShortDescription = model.ShortDescription, Remark = model.Remark, Price = model.Price, CurrentDiscount = model.CurrentDiscount, DiscountStartTime = model.DiscountStartTime, DiscountEndTime = model.DiscountEndTime, PublishTime = model.PublishTime, IsSeasonalProduct = model.IsSeasonalProduct, SaleStartTime = model.SaleStartTime, SaleEndTime = model.SaleEndTime, CategoryId = model.ProductCategoryForProduct, Properties = productPropertis }; //store in db and get the id db.Products.Add(product); db.SaveChanges(); if (model.CoverImg != null) { var uploadedFile = new UploadedFile(model.CoverImg); var coverImgName=uploadedFile.SaveAsWithGuid(Server.MapPath("~/ImgRepository/ProductImgs/" + product.Id)); var pathRel = Url.Content("~/ImgRepository/ProductImgs/" + product.Id + "/" + coverImgName); product.CoverImgUrl = pathRel; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } //update ProductId for each uploaded ProductImg if (!string.IsNullOrEmpty(model.UploadImgsIdString)) { var uploadedImgsIdArray = model.UploadImgsIdString.Split(','); foreach (var item in uploadedImgsIdArray) { if (!string.IsNullOrEmpty(item)) { var productImg = db.ProductImages.Where(pImage => pImage.Id.ToString() == item).Single(); //move to product's folder var imgName = UploadedFile.UploadedFileMoveTo(productImg.Url, Path.Combine(Server.MapPath("~/ImgRepository/ProductImgs/" + product.Id), (new FileInfo(productImg.Url)).Name)); var newPathRel= Url.Content("~/ImgRepository/ProductImgs/" + product.Id + "/" + imgName); productImg.Url = newPathRel; productImg.ProductId = product.Id; db.SaveChanges(); } } } return RedirectToAction("Index"); } model.ProductCategories = db.ProductCategories.ToList(); model.ProductProperties = db.ProductProperties.ToList(); return View(model); }
public ActionResult Edit(ProductManageEditViewModel model) { if (ModelState.IsValid) { //save cover img string newCoverImgUrl; if(model.CoverImg!=null) { var uploadedFile = new UploadedFile(model.CoverImg); var coverImgName = uploadedFile.SaveAsWithGuid(Server.MapPath("~/ImgRepository/ProductImgs/" + model.Id)); var pathRel = Url.Content("~/ImgRepository/ProductImgs/" + model.Id+"/"+coverImgName); newCoverImgUrl = pathRel; } else { newCoverImgUrl = db.Products.Find(model.Id).CoverImgUrl; } //update ProductId for each uploaded ProductImg if(!string.IsNullOrEmpty(model.UploadImgsIdString)) { var uploadedImgsIdArray = model.UploadImgsIdString.Split(','); foreach (var item in uploadedImgsIdArray) { if (!string.IsNullOrEmpty(item)) { var productImg = db.ProductImages.Where(pImage => pImage.Id.ToString() == item).Single(); //move to product's folder var imgName = UploadedFile.UploadedFileMoveTo(productImg.Url, Path.Combine(Server.MapPath("~/ImgRepository/ProductImgs/" + model.Id), (new FileInfo(productImg.Url)).Name)); var newPathRel = Url.Content("~/ImgRepository/ProductImgs/" + model.Id + "/" + imgName); productImg.Url = newPathRel; productImg.ProductId = model.Id; db.SaveChanges(); } } } if(!string.IsNullOrEmpty(model.DeletedImgsFileNameString)) { var deletedImgsFileNameArray = model.DeletedImgsFileNameString.Split(','); foreach(var item in deletedImgsFileNameArray) { if(!string.IsNullOrEmpty(item)) { var deletedImgList=db.ProductImages.Where(pImage => pImage.Name == item && pImage.ProductId == model.Id).ToList(); foreach(var deletedImg in deletedImgList) { db.ProductImages.Remove(deletedImg); db.SaveChanges(); } } } } //find properties in db var productPropertis = db.ProductProperties.Where(p => model.ProductPropertyForProduct.Contains(p.Id)).ToList(); var product = db.Products.Find(model.Id); product.Name = model.Name; product.ShortDescription = model.ShortDescription; product.Description = model.Description; product.Remark = model.Remark; product.CoverImgUrl = newCoverImgUrl; product.Price = model.Price; product.CurrentDiscount = model.CurrentDiscount; product.DiscountStartTime = model.DiscountStartTime; product.DiscountEndTime = model.DiscountEndTime; product.PublishTime = product.PublishTime; product.IsSeasonalProduct = model.IsSeasonalProduct; product.SaleStartTime = model.SaleStartTime; product.SaleEndTime = product.SaleEndTime; product.CategoryId = model.ProductCategoryForProduct; //many to many relationship solution product.Properties.Clear(); product.Properties = productPropertis; db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } model.ProductCategories = db.ProductCategories.ToList(); model.ProductProperties = db.ProductProperties.ToList(); return View(model); }
public ActionResult Create(ArticleManageCreateViewModel model) { if (ModelState.IsValid) { for (int i = 0; i < model.ArticleDialogs.Count; i++) { model.ArticleDialogs[i].SequenceNumber = i + 1; } var article = new Article { Title = model.Title, CoverImgUrl = model.CoverImgUrl, ShortDescription = model.ShortDescription, Description = model.Description, Content = model.Content, Meta = model.Meta, UrlSlug = model.UrlSlug, IsPublished = model.IsPublished, PostTime = model.PostTime, ModifyTime = model.ModifyTime, CategoryId=db.ArticleCategories.Find(model.ArticleCategoryForArticle).Id, Category= db.ArticleCategories.Find(model.ArticleCategoryForArticle), Dialogs= model.ArticleDialogs }; db.Articles.Add(article); db.SaveChanges(); if (model.CoverImg != null) { var uploadedFile = new UploadedFile(model.CoverImg); var coverImgName = uploadedFile.SaveAsWithGuid(Server.MapPath("~/ImgRepository/ArticleImgs/" + article.Id)); var pathRel = Url.Content("~/ImgRepository/ArticleImgs/" + article.Id + "/" + coverImgName); article.CoverImgUrl = pathRel; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); } var tagsList = ArticleHandler.ConvertTagsStringToList(model.ArticleTagsForArticle); //check tags and save foreach (var tagName in tagsList) { if (!db.ArticleTags.Any(t => t.Name == tagName)) { var articleTag = new ArticleTag { Name = tagName }; db.ArticleTags.Add(articleTag); db.SaveChanges(); } } article.Tags = db.ArticleTags.Where(t => tagsList.Contains(t.Name)).ToList(); db.Entry(article).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(model); }
public ActionResult Edit(ArticleManageEditViewModel model) { if (ModelState.IsValid) { var tagsList = ArticleHandler.ConvertTagsStringToList(model.ArticleTagsForArticle); //check tags and save foreach(var tagName in tagsList) { if (!db.ArticleTags.Any(t => t.Name ==tagName)) { var articleTag = new ArticleTag { Name = tagName }; db.ArticleTags.Add(articleTag); db.SaveChanges(); } } string newCoverImgUrl; if (model.CoverImg != null) { var uploadedFile = new UploadedFile(model.CoverImg); var coverImgName = uploadedFile.SaveAsWithGuid(Server.MapPath("~/ImgRepository/ArticleImgs/" + model.Id)); var pathRel = Url.Content("~/ImgRepository/ArticleImgs/" + model.Id + "/" + coverImgName); newCoverImgUrl = pathRel; } else { newCoverImgUrl = db.Articles.Find(model.Id).CoverImgUrl; } var article = db.Articles.Find(model.Id); article.Id = model.Id; article.Title = model.Title; article.CoverImgUrl = newCoverImgUrl; article.ShortDescription = model.ShortDescription; article.Description = model.Description; article.Content = model.Content; article.Meta = model.Meta; article.UrlSlug = model.UrlSlug; article.IsPublished = model.IsPublished; article.PostTime = model.PostTime; article.ModifyTime = model.ModifyTime; article.CategoryId = db.ArticleCategories.Find(model.ArticleCategoryForArticle).Id; article.Category = db.ArticleCategories.Find(model.ArticleCategoryForArticle); article.Tags.Clear(); article.Tags = db.ArticleTags.Where(t => tagsList.Contains(t.Name)).ToList(); article.Dialogs.Clear(); db.ArticleDialogs.Where(a => a.ArticleId == article.Id).ToList().ForEach(ad => db.ArticleDialogs.Remove(ad)); for (int i = 0; i < model.ArticleDialogs.Count; i++) { model.ArticleDialogs[i].SequenceNumber = i + 1; } article.Dialogs = model.ArticleDialogs; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(model); }