public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(product);
        }
        public void ProductController_Create_isNotValid()
        {
            Product testProduct = new Product();
            testProduct.productBrandName = "invalidTestBrand";
            ProductController controller = new ProductController();
            controller.ModelState.AddModelError("", "error message");

            var result = controller.Create(testProduct) as ViewResult;
            Product resultProduct = (Product)result.Model;

            Assert.AreEqual("invalidTestBrand", resultProduct.productBrandName);
        }
        public void ProductController_Create_isValid()
        {
            Product testProduct = new Product();
            testProduct.productBrandName = "testBrand";
            testProduct.productCostPrice = 1;
            testProduct.productRetailPrice = 2;
            testProduct.productQty = 5;
            ProductController controller = new ProductController();

            var result = (RedirectToRouteResult)controller.Create(testProduct);

            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public ActionResult CreateFromFile()
        {
            HttpPostedFileBase file = Request.Files[0];

            // if the file is not the products.csv file, warn that its not the demo file
            Debug.Assert(file.FileName == "products.csv", "Not using the demo file");

            //ensure file is not empty
            if (file != null && file.ContentLength > 0)
            {

                Debug.Print("Reading in the contents of the file");

                //ensure file is csv
                if (file.ContentType == "text/csv" || file.ContentType == "application/vnd.ms-excel")
                {
                    //read in the first line of the file
                    StreamReader sr = new StreamReader(file.InputStream);
                    string productLine = sr.ReadLine();
                    int count = 0;
                    //read in each line of the file
                    while (productLine != null)
                    {
                        //populate a new product with values
                        string[] pArray = productLine.Split(',');
                        Product newProd = new Product();
                        newProd.productBrandName = pArray[0];
                        newProd.productCostPrice = decimal.Parse(pArray[1]);
                        newProd.productRetailPrice = decimal.Parse(pArray[2]);
                        newProd.productQty = short.Parse(pArray[3]);

                        //add the product to the db
                        db.Products.Add(newProd);
                        db.SaveChanges();
                        count++;
                        productLine = sr.ReadLine();
                    }
                    return RedirectToAction("Index");
                }
            }

            return View();
        }
 public ActionResult Edit(Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(product);
 }