public IActionResult Create(Product p) { if (ModelState.IsValid) { ProductDb.Add(p, context); ViewData["Message"] = $"{p.Name} was added!"; return(View()); } //Show web page with errors return(View(p)); }
public async Task <bool> UpdateProducts(IList <ProductRepoModel> products) { if (products == null || products.Count == 0) { return(false); } try { foreach (ProductRepoModel productModel in products) { if (productModel != null) { var product = _context.Products.FirstOrDefault(p => p.ProductId == productModel.ProductId); if (product == null) { product = _mapper.Map <Product>(productModel); _context.Add(product); } else { product.Name = productModel.Name; product.Description = productModel.Description; product.Price = productModel.Price; product.Quantity = productModel.Quantity; product.BrandId = productModel.BrandId; product.CategoryId = productModel.CategoryId; } } } } catch (DbUpdateConcurrencyException) { return(false); } await _context.SaveChangesAsync(); return(true); }
public void ProductAdd(string type, int amount, string weight, DateTime expiration, byte[] image, string name) { ProductDb connection = new ProductDb(); connection.Add(type, amount, weight, expiration, image, name); }
public void ProcessRequest(HttpContext context) { bool result = false; string action = context.Request["action"] ?? ""; if (action == "select") { string id = context.Request["id"] ?? ""; string data = ProductDb.GetSingle(id); context.Response.ContentType = "application/json"; context.Response.Write(data); return; } else if (action == "list") { DataSet ds = ProductDb.GetList(""); DataTable table = ds.Tables[0]; Page page = new Page(); page.count = table.Rows.Count; page.data = table; page.code = 0; page.msg = ""; string data = JsonConvert.SerializeObject(page); context.Response.ContentType = "application/json"; context.Response.Write(data); return; } else if (action == "del") { string id = context.Request["id"] ?? ""; result = ProductDb.Delete(Convert.ToInt32(id)); } else { string title = context.Request["title"] ?? ""; string remark = context.Request["remark"] ?? ""; string content = context.Request["content"] ?? ""; string imgPath = context.Request["img"] ?? ""; Product product = new Product() { Title = title, Content = content, Remark = remark, ImgPath = imgPath, CreateTime = DateTime.Now, ModifyTime = DateTime.Now }; if (action == "add") { int ob = ProductDb.Add(product); context.Response.ContentType = "application/json"; context.Response.Write("{id:'" + ob + "'}"); return; } else { string id = context.Request["id"] ?? ""; product.Id = id; result = ProductDb.Update(product); context.Response.ContentType = "application/json"; context.Response.Write("{id:'" + id + "'}"); return; } } String message = result == true ? "操作成功" : "操作失败"; context.Response.ContentType = "text/plain"; context.Response.Write(message); }