示例#1
0
        public ActionResult Edit(Products products,  HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                Products p = db.Products.Find(products.productID);

               if (file != null &&  file.ContentLength > 0)
                {
                   string name = products.productName.Replace(" ", "_");
                   var dir = Path.Combine(Server.MapPath("~/Content"), name);
                   int htmlNum = 0 ;
                   products.version += 1;
                   products.mainPubLocation = products.location + "/" + saveAndExtracr(file, dir, ref htmlNum, products.version);
                   products.htmlNum = htmlNum;

                   p.version = products.version;
                   p.htmlNum = products.htmlNum;
                   p.mainPubLocation = products.mainPubLocation;
                }

               products.date = DateTime.Now;
               if (p.productName != products.productName)
                   p.productName = products.productName;

               if (p.productVersion != products.productVersion)
                   p.productVersion = products.productVersion;

               if (p.description != products.description)
                   p.description = products.description;

               if (p.roomID != products.roomID)
               {
                   p.roomID = products.roomID;
                   p.proom = db.rooms.Find(products.roomID);
               }

                //db.Entry(products).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Plist");
            }
            //print erroe
            foreach (var obj in ModelState.Values)
            {
                foreach (var error in obj.Errors)
                {
                    if (!string.IsNullOrEmpty(error.ErrorMessage))
                        System.Diagnostics.Debug.WriteLine("ERROR WHY = " + error.ErrorMessage);
                        System.Diagnostics.Debug.WriteLine(error.Exception);
                }
            }
            //ModelState.AddModelError("name", "nnnn");
            ViewBag.ro = (db.rooms.ToList());
            return View(products);
        }
示例#2
0
        public ActionResult Create(Products product, HttpPostedFileBase file)
        {
            if (ModelState.IsValid && file != null)
            {
                if (file.ContentLength > 0)
                {
                    string name = product.productName.Replace(" ", "_");
                    var dir = Path.Combine(Server.MapPath("~/Content"), name);
                    if(!Directory.Exists(dir))
                    {
                    Directory.CreateDirectory(dir);
                    product.version = 1;
                    int htmlNum = 0 ;
                    product.location = "Content/" + name;
                    product.mainPubLocation = product.location + "/" + saveAndExtracr(file, dir, ref htmlNum, product.version);

                    product.htmlNum = htmlNum;
                    product.date = DateTime.Now;

                    product.proom = db.rooms.Find(product.roomID);
                    db.Products.Add(product);
                    db.SaveChanges();
                    return RedirectToAction("Plist");
                    }
                   // ViewBag.fileError = "file with the ame name is rdy exists for this produrct";
                }
            }
            ViewBag.ro = (db.rooms.ToList());
            return View(product);
        }