public IActionResult Index() { ViewProductModel myModel = new ViewProductModel(); myModel.ProductList = getList(); myModel.CateList = GetCategory(); return(View(myModel)); }
public IActionResult DetailProduct(int ID) { ViewProductModel myModel = new ViewProductModel(); myModel.proEdit = GetProduct(ID); myModel.CateList = GetCategory(); myModel.Soluong = GetInfoWareHouse(ID); return(View(myModel)); }
public async Task <IActionResult> Edit(ProEdit proEdit, IFormFile pic) { ViewProductModel myModel = new ViewProductModel(); myModel.proEdit = proEdit; myModel.CateList = GetCategory(); if (ModelState.IsValid) { if (pic != null) { var file = pic; var uploads = Path.Combine(_appEnvironment.WebRootPath, "Images"); var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); string filePath = "Images\\" + fileName; proEdit.Image = filePath; } string connectionString = Configuration["ConnectionStrings:DefaultConnection"]; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("CapNhatSP", connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; if (connection.State == ConnectionState.Open) { connection.Close(); } connection.Open(); command.Parameters.AddWithValue("@MaSP", proEdit.MaSP); command.Parameters.AddWithValue("@TenSP", proEdit.productName); command.Parameters.AddWithValue("@MoTa", proEdit.Description); command.Parameters.AddWithValue("@MaLoai", proEdit.productType); command.Parameters.AddWithValue("@TrangThai", proEdit.Stock); command.Parameters.AddWithValue("@Gia", proEdit.Price); command.Parameters.AddWithValue("@MauSac", proEdit.Color); command.Parameters.AddWithValue("@hinhanh", proEdit.Image); command.Parameters.AddWithValue("@Modify", DateTime.Now); command.ExecuteNonQuery(); } } return(RedirectToAction("DetailProduct", "Product", new { id = Convert.ToInt32(proEdit.MaSP) })); } else { string connectionString = Configuration["ConnectionStrings:DefaultConnection"]; using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand("CapNhatSP", connection)) { command.CommandType = System.Data.CommandType.StoredProcedure; if (connection.State == ConnectionState.Open) { connection.Close(); } connection.Open(); int MaLoai = Convert.ToInt32(proEdit.productType); double Gia = Convert.ToDouble(proEdit.Price); command.Parameters.AddWithValue("@MaSP", proEdit.MaSP); command.Parameters.AddWithValue("@TenSP", proEdit.productName); command.Parameters.AddWithValue("@MoTa", proEdit.Description); command.Parameters.AddWithValue("@MaLoai", proEdit.productType); command.Parameters.AddWithValue("@TrangThai", proEdit.Stock); command.Parameters.AddWithValue("@Gia", proEdit.Price); command.Parameters.AddWithValue("@MauSac", proEdit.Color); command.Parameters.AddWithValue("@hinhanh", proEdit.Image); command.Parameters.AddWithValue("@Modify", DateTime.Now); command.ExecuteNonQuery(); } } return(RedirectToAction("DetailProduct", "Product", new { id = Convert.ToInt32(proEdit.MaSP) })); } } return(RedirectToAction("EditProduct", "Product", new { id = Convert.ToInt32(proEdit.MaSP) })); }