Example #1
0
        public CatalogView GetCatalogViewByCode(string code)
        {
            CatalogView catalogview = new CatalogView();

            Catalog catalog = db.Catalogs.Find(code);

            catalogview.Code = catalog.Code;
            catalogview.Name = catalog.Name;
            catalogview.Price = catalog.Price;
            catalogview.Description = catalog.Description;
            catalogview.CategoryId = catalog.CategoryId;
            catalogview.CategoryDetail = catalog.CategoryDetail;

            return catalogview;
        }
        public ActionResult Create(CatalogView catalogview)
        {
            if (ModelState.IsValid)
            {
                //foreach (string file in Request.Files)
                //{
                //    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                //    if (hpf.ContentLength == 0)
                //        continue;
                //    string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/images/", Path.GetFileName(hpf.FileName));
                //    hpf.SaveAs(savedFileName);
                //    catalog.ImageUrl = "~/Content/images/" + Path.GetFileName(hpf.FileName);
                //}

                Int32 length = catalogview.FileUpload.ContentLength;

                Catalog catalog = new Catalog();

                byte[] tempImage = new byte[length];
                catalogview.FileUpload.InputStream.Read(tempImage, 0, length);

                //fill in the catalog model
                catalog.Code = catalogview.Code;
                catalog.Name = catalogview.Name;
                catalog.Price = catalogview.Price;
                catalog.Description = catalogview.Description;
                catalog.CategoryId = catalogview.CategoryId;
                catalog.ImageData = tempImage;
                catalog.ContentType = catalogview.FileUpload.ContentType;

                catalogRepo.CreateCatalog(catalog);
                catalogRepo.Save();
                return RedirectToAction("Index");
            }

            return View(catalogview);
        }
        public ActionResult Edit(string code, CatalogView catalogview)
        {
            Catalog ctlg = catalogRepo.GetCatalogByCode(code);
            if (ModelState.IsValid)
            {
                //foreach (string file in Request.Files)
                //{
                //    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                //    if (hpf.ContentLength == 0)
                //        continue;
                //    string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/images/", Path.GetFileName(hpf.FileName));
                //    hpf.SaveAs(savedFileName);
                //    catalog.ImageUrl = "~/Content/images/" + Path.GetFileName(hpf.FileName);
                //}
                //if (ctlg.ImageUrl != "" && (catalog.ImageUrl == "" || catalog.ImageUrl == null))
                //{
                //    catalog.ImageUrl = ctlg.ImageUrl;
                //}

                Catalog catalog = new Catalog();

                catalog.Code = catalogview.Code;
                catalog.Name = catalogview.Name;
                catalog.Price = catalogview.Price;
                catalog.Description = catalogview.Description;
                catalog.CategoryId = catalogview.CategoryId;
                catalog.CategoryDetail = catalogview.CategoryDetail;

                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    Int32 length = file.ContentLength;

                    catalog.ContentType = file.ContentType;
                    byte[] tempImage = new byte[length];
                    file.InputStream.Read(tempImage, 0, length);
                    catalog.ImageData = tempImage;
                }
                else
                {
                    catalog.ImageData = ctlg.ImageData;
                    catalog.ContentType = ctlg.ContentType;
                }

                //db.Entry(catalog).State = EntityState.Modified;
                catalogRepo.EditCatalog(catalog, ctlg);
                catalogRepo.Save();
                return RedirectToAction("Index");
            }

            return View(catalogview);
        }