Exemple #1
0
 public ActionResult Input(string id = "")
 {
     try
     {
         if (string.IsNullOrEmpty(id))
         {
             ViewBag.Title  = "Create new product";
             ViewBag.Method = "Add";
             Product newProduct = new Product()
             {
                 ProductID = 0
             };
             ProductAttributeResult model = new ProductAttributeResult()
             {
                 Product           = newProduct,
                 ProductAttributes = null
             };
             return(View(model));
         }
         else
         {
             ViewBag.Title  = "Edit a product";
             ViewBag.Method = "Edit";
             Product editProduct           = CatalogBLL.GetProduct(Convert.ToInt32(id));
             List <ProductAttribute> attr  = CatalogBLL.ListOfProductAttribute(Convert.ToInt32(id));
             ProductAttributeResult  model = new ProductAttributeResult()
             {
                 Product           = editProduct,
                 ProductAttributes = attr
             };
             if (editProduct == null)
             {
                 return(RedirectToAction("Index"));
             }
             return(View(model));
         }
     }
     catch (Exception ex)
     {
         return(Content(ex.Message + ":" + ex.StackTrace));
     }
 }
Exemple #2
0
        public async Task <Result> Get(int id)
        {
            var productAttribute = await _productAttrRepository.Query()
                                   .Include(c => c.Group)
                                   .FirstOrDefaultAsync(c => c.Id == id);

            if (productAttribute == null)
            {
                return(Result.Fail("单据不存在"));
            }
            var model = new ProductAttributeResult
            {
                Id        = productAttribute.Id,
                Name      = productAttribute.Name,
                GroupId   = productAttribute.GroupId,
                GroupName = productAttribute.Group?.Name
            };

            return(Result.Ok(model));
        }
Exemple #3
0
        public ActionResult Input(Product model, HttpPostedFileBase uploadFile, string method)
        {
            ProductAttributeResult models = new ProductAttributeResult();

            try
            {
                //TODO: Kiểm tra tính hợp lệ của dữ liệu
                if (string.IsNullOrEmpty(model.ProductName))
                {
                    ModelState.AddModelError("ProductName", "ProductName expected!");
                }
                if (string.IsNullOrEmpty(model.QuantityPerUnit))
                {
                    ModelState.AddModelError("QuantityPerUnit", "QuantityPerUnit expected!");
                }
                if (double.IsNaN(model.UnitPrice))
                {
                    ModelState.AddModelError("UnitPrice", "UnitPrice expected!");
                }
                if (model.SupplierID == 0)
                {
                    ModelState.AddModelError("SupplierID", "SupplierID expected!");
                }
                if (model.CategoryID == 0)
                {
                    ModelState.AddModelError("CategoryID", "CategoryID expected!");
                }
                if (string.IsNullOrEmpty(model.Descriptions))
                {
                    model.Descriptions = "";
                }
                if (string.IsNullOrEmpty(model.PhotoPath))
                {
                    model.PhotoPath = "";
                }

                //Upload ảnh
                if (uploadFile != null)
                {
                    string folder = Server.MapPath("~/Images/uploads");
                    //Lấy phần mở rộng của file
                    string extensionName = System.IO.Path.GetExtension(uploadFile.FileName);
                    if (!extensionName.Equals(".jpg") && !extensionName.Equals(".JPG") && !extensionName.Equals(".png") && !extensionName.Equals(".PNG"))
                    {
                        ModelState.AddModelError("ExtensionName", "File type expected!");
                    }
                    else
                    {
                        // Tạo tên file ngẫu nhiên
                        string fileName = $"{DateTime.Now.Ticks}{Path.GetExtension(uploadFile.FileName)}";
                        string filePath = Path.Combine(folder, fileName);
                        uploadFile.SaveAs(filePath);

                        model.PhotoPath = fileName;
                    }
                }

                if (method == "Add")
                {
                    ViewBag.Title  = "Create new product";
                    ViewBag.Method = "Add";
                    Product newProduct = new Product()
                    {
                        ProductID = 0
                    };
                    models = new ProductAttributeResult()
                    {
                        Product           = newProduct,
                        ProductAttributes = null
                    };
                }
                else if (method == "Edit")
                {
                    ViewBag.Title  = "Edit a product";
                    ViewBag.Method = "Edit";
                    Product editProduct          = CatalogBLL.GetProduct(Convert.ToInt32(model.ProductID));
                    List <ProductAttribute> attr = CatalogBLL.ListOfProductAttribute(Convert.ToInt32(model.ProductID));
                    models = new ProductAttributeResult()
                    {
                        Product           = editProduct,
                        ProductAttributes = attr
                    };
                }

                if (!ModelState.IsValid)
                {
                    return(View(models));
                }

                //TODO: Lưu dữ liệu vào DB
                if (model.ProductID == 0)
                {
                    CatalogBLL.AddProduct(model);
                }
                else
                {
                    CatalogBLL.UpdateProduct(model);
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message + ":" + ex.StackTrace);
                return(View(models));
            }
        }