Exemplo n.º 1
0
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                Product pro = new Product();
                pro.Name       = product.Name;
                pro.SmallDesc  = product.SmallDesc;
                pro.megaDesc   = product.megaDesc;
                pro.CreateDate = DateTime.Now;
                db.Product.Add(pro);
                db.SaveChanges();

                foreach (var item in product.CategoryId)
                {
                    ProCategory cat = new ProCategory();
                    cat.ProductId          = pro.Id;
                    cat.SubOfSubcategoryId = item;

                    db.ProCategory.Add(cat);
                    db.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            ViewBag.Category = db.SubOfSubcategory.ToList();
            return(View(product));
        }
Exemplo n.º 2
0
        public ActionResult Edit(Product product)
        {
            Product pro = db.Product.Find(product.Id);

            if (ModelState.IsValid)
            {
                pro.Name            = product.Name;
                pro.SmallDesc       = product.SmallDesc;
                pro.megaDesc        = product.megaDesc;
                db.Entry(pro).State = EntityState.Modified;

                ProCategory cat = db.ProCategory.FirstOrDefault(p => p.ProductId == product.Id);

                foreach (var item in product.CategoryId)
                {
                    cat.SubOfSubcategoryId = item;

                    db.Entry(cat).State = EntityState.Modified;
                }
                db.SaveChanges();


                return(RedirectToAction("Index"));
            }
            ViewBag.Category = db.SubOfSubcategory.ToList();
            return(View(product));
        }
        /// <summary>
        /// This method retrieve product categories record from database
        /// </summary>
        /// <returns> List of product categories </returns>
        public List <ProCategory> GetAllCategories()
        {
            List <ProCategory> lstC = new List <ProCategory>();

            try
            {
                //To Do Select All
                //Configure command for select all
                cmd             = new SqlCommand();
                cmd.CommandText = "select cname from procategory";
                //Attach the connection with the command
                cmd.Connection = con;
                //open connection
                con.Open();
                //Execute the command
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    ProCategory c = new ProCategory
                    {
                        //CategoryId = (int)sdr[0],
                        CategoryName = sdr[0].ToString()
                    };
                    lstC.Add(c);
                }
                //connection close
                sdr.Close();
            }
            catch (SqlException ex)
            {
                throw new OnlineSoppingException(ex.Message);
            }
            catch (Exception ex)
            {
                throw new OnlineSoppingException(ex.Message);
            }
            finally
            {
                //Connection close
                con.Close();
            }
            return(lstC);
        }