Esempio n. 1
0
        public ActionResult Create(AdProductViewModel Adproduct)
        {
            if (ModelState.IsValid)
            {
                if (Adproduct != null)
                {
                    //Add new ReadOnlyAttribute
                    var newReadOnlyAttribute = new ReadOnlyAttribute();
                    {
                        var user = User.Identity.GetUserName();
                        newReadOnlyAttribute.LastModified = DateTime.Now;
                        newReadOnlyAttribute.Created      = DateTime.Now;
                        newReadOnlyAttribute.Version      = 0;
                    }


                    db.ReadOnlyAttribute.Add(newReadOnlyAttribute);
                    db.SaveChanges();

                    //Add new Product
                    Adproduct.Product.ReadOnlyAttributeId = newReadOnlyAttribute.ReadOnlyAttributeId;
                    Adproduct.Product.Created             = DateTime.Now;
                    Adproduct.Product.SubcategoryId       = Adproduct.SubcategoryId;

                    db.Products.Add(Adproduct.Product);
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }


            return(View());
        }
Esempio n. 2
0
        public ActionResult CreateAd(AdProductViewModel adProduct)
        {
            var isSuchTitleExist = _context.Ads.FirstOrDefault(c => c.Title == adProduct.Title);

            //if an ad with the same title already exists, send back to createAd form
            if (isSuchTitleExist != null)
            {
                return(RedirectToAction("Index"));
            }

            //adding a new Ad to the database and saving
            var newAd = new Ad()
            {
                Description = adProduct.AdDescription,
                Image       = adProduct.Image,
                Title       = adProduct.Title,
                Date        = adProduct.AdDate
            };

            _context.Ads.Add(newAd);
            _context.SaveChanges();

            //finding the ad id by locating it from the database
            var newAddedAd = _context.Ads.FirstOrDefault(c => c.Title == newAd.Title);
            //adding a new product to the database and saving
            var newProduct = new Product()
            {
                Description = adProduct.productDescription,
                AdId        = newAddedAd.Id,
                Publisher   = adProduct.productPublisher,
                firstImage  = adProduct.productFirstImage,
                secondImage = adProduct.productSecondImage,
                Price       = adProduct.productPrice
            };

            _context.Products.Add(newProduct);
            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }