public ActionResult EditProduct(int id) { AppDbContext dbContext = new AppDbContext(); ListProductsViewModel productDetail = dbContext.GetProducts.SingleOrDefault(p => p.Id == id); AddProductDetailsViewModel product = new AddProductDetailsViewModel { Name = productDetail.Name, Brand = productDetail.Brand, Category = productDetail.Category, ProductId = productDetail.Id }; return(View(product)); }
public ActionResult EditProduct(HttpPostedFileBase PhotoPath, AddProductDetailsViewModel model) { if (ModelState.IsValid) { AppDbContext dbContext = new AppDbContext(); ListProductsViewModel productDetail = dbContext.GetProducts.SingleOrDefault(p => p.Id == model.ProductId); var fileName = Path.GetFileName(PhotoPath.FileName); var path = "/Public/Products/" + productDetail.Name; var photoUrl = Server.MapPath(path); var photoTitle = Path.GetFileNameWithoutExtension(PhotoPath.FileName); var uniqName = Guid.NewGuid().ToString() + "_" + fileName; if (!Directory.Exists(photoUrl)) { Directory.CreateDirectory(photoUrl); } var photoPath = Path.Combine(photoUrl, uniqName); PhotoPath.SaveAs(photoPath); AddProductDetailsViewModel product = new AddProductDetailsViewModel { Price = model.Price, Color = model.Color, Storage = model.Storage, Processor = model.Processor, Memory = model.Memory, Display = model.Display, CreatedBy = User.Identity.Name, CreatedDate = DateTime.Now, PhotoPath = photoPath, PhotoTitle = photoTitle.ToString(), PhotoSrc = path + '/' + uniqName, ProductId = model.ProductId }; dbContext.AddProductDetails(product); return(RedirectToAction("Products")); } return(View()); }
public void AddProductDetails(AddProductDetailsViewModel productDetails) { string cs = ConfigurationManager.ConnectionStrings["EComMgt"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("addProductDetails", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter paramPrice = new SqlParameter(); paramPrice.ParameterName = "@Price"; paramPrice.Value = productDetails.Price; cmd.Parameters.Add(paramPrice); SqlParameter paramColor = new SqlParameter(); paramColor.ParameterName = "@Color"; paramColor.Value = productDetails.Color; cmd.Parameters.Add(paramColor); SqlParameter paramStorage = new SqlParameter(); paramStorage.ParameterName = "@Storage"; paramStorage.Value = productDetails.Storage; cmd.Parameters.Add(paramStorage); SqlParameter paramProcessor = new SqlParameter(); paramProcessor.ParameterName = "@Processor"; paramProcessor.Value = productDetails.Processor; cmd.Parameters.Add(paramProcessor); SqlParameter paramMemory = new SqlParameter(); paramMemory.ParameterName = "@Memory"; paramMemory.Value = productDetails.Memory; cmd.Parameters.Add(paramMemory); SqlParameter paramDisplay = new SqlParameter(); paramDisplay.ParameterName = "@Display"; paramDisplay.Value = productDetails.Display; cmd.Parameters.Add(paramDisplay); SqlParameter paramProductId = new SqlParameter(); paramProductId.ParameterName = "@ProductId"; paramProductId.Value = productDetails.ProductId; cmd.Parameters.Add(paramProductId); SqlParameter paramCreatedBy = new SqlParameter(); paramCreatedBy.ParameterName = "@CreatedBy"; paramCreatedBy.Value = productDetails.CreatedBy; cmd.Parameters.Add(paramCreatedBy); SqlParameter paramCreatedDate = new SqlParameter(); paramCreatedDate.ParameterName = "@CreatedDate"; paramCreatedDate.Value = productDetails.CreatedDate; cmd.Parameters.Add(paramCreatedDate); SqlParameter paramPhotoPath = new SqlParameter(); paramPhotoPath.ParameterName = "@Path"; paramPhotoPath.Value = productDetails.PhotoPath; cmd.Parameters.Add(paramPhotoPath); SqlParameter paramPhotoTitle = new SqlParameter(); paramPhotoTitle.ParameterName = "@Title"; paramPhotoTitle.Value = productDetails.PhotoTitle; cmd.Parameters.Add(paramPhotoTitle); SqlParameter paramPhotoSrc = new SqlParameter(); paramPhotoSrc.ParameterName = "@Src"; paramPhotoSrc.Value = productDetails.PhotoSrc; cmd.Parameters.Add(paramPhotoSrc); con.Open(); cmd.ExecuteNonQuery(); } }