public IActionResult Edit(Product pro) { if (ModelState.IsValid) { context.Update(pro); context.SaveChanges(); ViewData["Message"] = "Product Updated!"; } return(View(pro)); }
public ActionResult Delete(int id) { var customerid = Session["Id"]; Customer c = db.Customers.Find(customerid); CartDetail deleted = db.CartDetails.Find(id); c.Cart.CartDetail.Remove(deleted); db.Entry(c).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create([Bind(Include = "Id,NameSurname,Email,PhoneNumber,Address")] Customer customer) { if (ModelState.IsValid) { db.Customers.Add(customer); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customer)); }
public ActionResult Create([Bind(Include = "CategoryId,Name")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public IActionResult Edit(Product p) { if (ModelState.IsValid) { context.Update(p); context.SaveChanges(); ViewData["Message"] = "Product Updated!"; //Return same page with message, or redirect to another page return(View(p)); } //Return view with errors return(View(p)); }
public JsonResult Customer(Customer customer, string Password, string Password2) { if (Password == Password2) { if (ModelState.IsValid) { db.Customers.Add(customer); db.SaveChanges(); RedirectToAction("Customer"); return(Json(true)); } } return(Json(false)); }
public ActionResult Upload(HttpPostedFileBase productFile) { string path = null; if (productFile.ContentLength > 0) { var filename = Path.GetFileName(productFile.FileName); path = AppDomain.CurrentDomain.BaseDirectory + "Upload\\" + filename; productFile.SaveAs(path); var csv = new CsvReader(new StreamReader(path)); csv.Configuration.RegisterClassMap <ProductMap>(); var productList = csv.GetRecords <Product>(); foreach (var product in productList) { product.Date = DateTime.Now; db.Products.Add(product); } db.SaveChanges(); } return(View()); }
public static void Initialize(CommerceContext commerceContext) { commerceContext.Database.EnsureCreated(); commerceContext.Products.Add(new Product("Cup", new Money(4, 50))); commerceContext.Products.Add(new Product("Pen", new Money(1, 30))); commerceContext.Products.Add(new Product("Plate", new Money(4, 70))); commerceContext.SaveChanges(true); }
public ActionResult Create([Bind(Include = "Id,ParentId,Name")] Category category, HttpPostedFileBase Image) { string folder = Server.MapPath("/Uploads/CategoryImage/"); Image.SaveAs(folder + Image.FileName); category.ImageUrl = "/Uploads/CategoryImage/" + Image.FileName; if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Categories = db.Categories.Where(x => x.ParentId == null).ToList(); //ViewBag.ParentId = new SelectList(db.Categories, "Id", "Name", category.ParentId); return(View(category)); }
public MockCommerceContext(string dataBasename) { var options = new DbContextOptionsBuilder <CommerceContext>() .UseInMemoryDatabase(databaseName: dataBasename) .Options; context = new CommerceContext(options); context.Users.Add(new User { Id = 1, Username = "******", DisplayName = "aDisplayName", CreationDate = DateTime.Now, EmailAddress = "*****@*****.**", Password = "******" }); context.Users.Add(new User { Id = 2, Username = "******", DisplayName = "DisplayName2", CreationDate = DateTime.Now.AddDays(-50), EmailAddress = "*****@*****.**", Password = "******" }); context.Users.Add(new User { Id = 3, Username = "******", DisplayName = "mDisplaName3", CreationDate = DateTime.Now.AddYears(-1), EmailAddress = "*****@*****.**", Password = "******" }); context.Orders.Add(new Order { Id = 1, UserId = 1 }); context.Orders.Add(new Order { Id = 2, UserId = 2 }); context.Orders.Add(new Order { Id = 3, UserId = 3 }); context.Products.Add(new Product { Id = 1, CreationDate = DateTime.Now, Description = "Product description 1", Name = "Product1", Price = 10.20M }); context.Products.Add(new Product { Id = 2, CreationDate = DateTime.Now.AddDays(-50), Description = "Product description 2", Name = "Product2", Price = 20.20M }); context.Products.Add(new Product { Id = 3, CreationDate = DateTime.Now.AddYears(-1), Description = "Product description 3", Name = "Product3", Price = 30.20M }); context.ProductOrders.Add(new ProductOrder { OrderId = 1, ProductId = 1, Quantity = 3, UnityPrice = 10.20M }); context.ProductOrders.Add(new ProductOrder { OrderId = 1, ProductId = 2, Quantity = 1, UnityPrice = 20.20M }); context.ProductOrders.Add(new ProductOrder { OrderId = 2, ProductId = 1, Quantity = 3, UnityPrice = 10.20M }); context.ProductOrders.Add(new ProductOrder { OrderId = 2, ProductId = 2, Quantity = 2, UnityPrice = 20.20M }); context.ProductOrders.Add(new ProductOrder { OrderId = 2, ProductId = 3, Quantity = 1, UnityPrice = 30.20M }); context.SaveChanges(); }
public async Task <IActionResult> AddProduct(DashboardModel model) { if (ModelState.IsValid) { var check = _ccontext.products.SingleOrDefault(p => p.name == model.product.name); using (var stream = new MemoryStream()) { await model.images.CopyToAsync(stream); model.product.images = stream.ToArray(); } //check if product exist, if yes, just update it if (check == null) { Product product = model.product; _ccontext.Add(product); _ccontext.SaveChanges(); } else { check.quantity = model.product.quantity; check.price = model.product.price; check.updated_at = DateTime.Now; check.images = model.product.images; // check.image = model.product.image; _ccontext.SaveChanges(); } return(RedirectToAction("Products")); } List <Product> products = _ccontext.products.ToList(); DashboardModel newmodel = new DashboardModel() { products = products, }; return(View("Products", newmodel)); }
public ActionResult Create(HttpPostedFileBase ImagePath, [Bind(Include = "ProductID,CategoryID,Name,Description,UnitPrice,Quantity,ImagePath,Status")] Product product) { var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; if (ImagePath == null || ImagePath.ContentLength == 0) { ModelState.AddModelError("ImageUpload", "This field is required"); } else if (!validImageTypes.Contains(ImagePath.ContentType)) { ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image."); } if (ModelState.IsValid) { if (ImagePath != null && ImagePath.ContentLength > 0) { var uploadDir = "~/Upload"; var imagePath = Path.Combine(Server.MapPath(uploadDir), ImagePath.FileName); var imageUrl = Path.Combine(uploadDir, ImagePath.FileName); ImagePath.SaveAs(imagePath); product.ImagePath = ImagePath.FileName; } product.Date = DateTime.Now; db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryID = new SelectList(db.Categories, "CategoryId", "Name", product.CategoryID); return(View(product)); }
public ActionResult Create(Product newProduct, HttpPostedFileBase[] productImage, int CategoryIds, Size sizes) { newProduct.CategoryId = CategoryIds; newProduct.Size = sizes; newProduct.ProductImages = new List <ProductImage>(); foreach (var item in productImage) { ProductImage p = new ProductImage(); p.ImageURL = item.FileName; item.SaveAs(Server.MapPath("/Uploads/Product/") + item.FileName); newProduct.ProductImages.Add(p); } if (ModelState.IsValid) { db.Products.Add(newProduct); db.SaveChanges(); } ViewBag.Categories = db.Categories.ToList(); return(View()); }
public IActionResult Register(RegLogModel model) { PasswordHasher <RegUser> hasher = new PasswordHasher <RegUser>(); if (ModelState.IsValid) { model.reguser.password = hasher.HashPassword(model.reguser, model.reguser.password); Customer user = new Customer() { first_name = model.reguser.first_name, last_name = model.reguser.last_name, email = model.reguser.email, password = model.reguser.password }; _ccontext.customers.Add(user); _ccontext.SaveChanges(); TempData["reg"] = "Register successfully, please login !"; return(RedirectToAction("Home")); } return(View("Home", model)); }
public int Complete() { return _context.SaveChanges(); }
public TEntity Add(TEntity entity) { Context.Set <TEntity>().Add(entity); Context.SaveChanges(); return(entity); }
public async Task CreateProductAsync(Product product) { await _csContext.Products.AddAsync(product); _csContext.SaveChanges(); }
public long Save(Product product) { db.Products.Add(product); db.SaveChanges(); return(product.ID); }
public IActionResult AddProduct(Product prod) { _context.products.Add(prod); _context.SaveChanges(); return(RedirectToAction("Products")); }