Exemple #1
0
        public async Task <ActionResult> Create()
        {
            var p = new RelatedProductVM();

            await PopulateSelectLists();

            return(View(p));
        }
        public async Task CreateRelated(RelatedProductVM rp)
        {
            var fromProduct = await db.Products.SingleOrDefaultAsync(x => x.ProductId == rp.FromProductId);

            var toProduct = await db.Products.SingleOrDefaultAsync(x => x.ProductId == rp.ToProductId);

            if (fromProduct == null || toProduct == null)
            {
                throw new ArgumentException("Both products are required");
            }
            if (fromProduct.ProductId == toProduct.ProductId)
            {
                throw new ArgumentException("Cannot create a relationship between the same product");
            }
            fromProduct.RelatedToProducts.Add(toProduct);
            await db.SaveChangesAsync();
        }
Exemple #3
0
        public async Task <ActionResult> Create(RelatedProductVM p)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _ps.CreateRelated(p);

                    return(RedirectToAction("Index"));
                }
                catch (ArgumentException aex)
                {
                    ModelState.AddModelError(string.Empty, aex.Message);
                }
                catch (Exception)
                {
                    ModelState.AddModelError(string.Empty, "This relationship already exists");
                }
            }
            await PopulateSelectLists();

            return(View(p));
        }