public IActionResult Create(ApplicationType app)
 {
     if (ModelState.IsValid)
     {
         _db.Add(app);
         _db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(app));
 }
Пример #2
0
        public IActionResult Create(Category cat)
        {
            _db.Add(cat);
            _db.SaveChanges();
            return(RedirectToAction("Index"));

            //Using ServerSide Validation
            //if (ModelState.IsValid)
            //{
            //    _db.Add(cat);
            //    _db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
            //return View(cat);
        }
Пример #3
0
        public IActionResult Upsert(ProductVM productVM)
        {
            if (ModelState.IsValid)
            {
                var    files       = HttpContext.Request.Form.Files;
                string webRootPath = _webHostEnvironment.WebRootPath;

                if (productVM.Product.Id == 0)
                {
                    //Creating
                    string upload    = webRootPath + WC.ImagePath;
                    string fileName  = Guid.NewGuid().ToString();
                    string extension = Path.GetExtension(files[0].FileName);

                    using (var fileStream = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create))
                    {
                        files[0].CopyTo(fileStream);
                    }

                    productVM.Product.Image = fileName + extension;

                    _db.Product.Add(productVM.Product);
                }
                else
                {
                    //updating
                    var objFromDb = _db.Product.AsNoTracking().FirstOrDefault(u => u.Id == productVM.Product.Id);

                    if (files.Count > 0)
                    {
                        string upload    = webRootPath + WC.ImagePath;
                        string fileName  = Guid.NewGuid().ToString();
                        string extension = Path.GetExtension(files[0].FileName);

                        var oldFile = Path.Combine(upload, objFromDb.Image);

                        if (System.IO.File.Exists(oldFile))
                        {
                            System.IO.File.Delete(oldFile);
                        }

                        using (var fileStream = new FileStream(Path.Combine(upload, fileName + extension), FileMode.Create))
                        {
                            files[0].CopyTo(fileStream);
                        }

                        productVM.Product.Image = fileName + extension;
                    }
                    else
                    {
                        productVM.Product.Image = objFromDb.Image;
                    }
                    _db.Product.Update(productVM.Product);
                }


                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            productVM.CategoryDropDownList = _db.Category.Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.Id.ToString()
            });
            productVM.ApplicationTypeDropDownList = _db.ApplicationType.Select(i => new SelectListItem
            {
                Text  = i.Name,
                Value = i.Id.ToString()
            });
            return(View(productVM));
        }