Example #1
0
        public ActionResult Update(FormCollection col)
        {
            bool   isValid = true;
            string id      = col["id"];
            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.UpdateProduct(p);
                if (isOk)
                {
                    return(GetDetail(col));
                }
                else
                {
                    ViewBag.Error = "Update Failed!";
                    return(RedirectToAction("Error"));
                }
            }
            else
            {
                return(Edit(col));
            }
        }