コード例 #1
0
        public ActionResult AddPhone(FormCollection col)
        {
            bool   isValid = true;
            string id      = (dao.GetAllProduct().Count + 1).ToString();
            string name    = col["name"];

            if (name == null || name.Equals(""))
            {
                isValid      = false;
                ViewBag.NERR = "Invalid name!";
            }
            string priceString = col["price"];

            if (!System.Text.RegularExpressions.Regex.IsMatch(priceString, "[0-9]{1,15}"))
            {
                isValid      = false;
                ViewBag.PERR = "Invalid Price!";
            }
            string quantityString = col["quantity"];

            if (!System.Text.RegularExpressions.Regex.IsMatch(quantityString, "[0-9]{1,7}"))
            {
                isValid      = false;
                ViewBag.QERR = "Invalid Quantity!";
            }
            string status  = col["status"];
            string cateID  = col["category"];
            string brandID = col["brand"];
            string img     = col["img"];

            if (img == null || img.Equals(""))
            {
                img = "https://previews.123rf.com/images/doomko/doomko1508/doomko150800003/43683599-fun-mobile-phone-cartoon-with-thumbs-up.jpg";
            }
            if (isValid)
            {
                Product p    = new Product(id, name, Convert.ToInt32(quantityString), Convert.ToInt32(priceString), status, brandID, cateID, img);
                bool    isOk = dao.AddProduct(p);
                if (isOk)
                {
                    return(LoadList());
                }
                else
                {
                    ViewBag.Error = "Create Failed!";
                    return(RedirectToAction("Error"));
                }
            }
            else
            {
                ViewBag.Name     = name;
                ViewBag.Price    = priceString;
                ViewBag.Quantity = quantityString;
                ViewBag.Image    = img;
                return(CreatePhone(col));
            }
        }