예제 #1
0
        public async Task <ActionResult> Create(ViewModels.Computer addComputer)
        {
            if (ModelState.IsValid)
            {
                var brand = await db.Brands.Where(x => x.IdBrand == addComputer.Brand).SingleOrDefaultAsync();

                var processor = await db.Processors.Where(x => x.IdProcessor == addComputer.Processor).SingleOrDefaultAsync();

                var computer = new Models.Computer()
                {
                    SKU            = addComputer.SKU,
                    Title          = addComputer.Title,
                    Description    = addComputer.Description,
                    Brand          = brand,
                    Processor      = processor,
                    ProductionYear = addComputer.ProductionYear,
                    ScreenSize     = addComputer.ScreenSize,
                    RAM            = addComputer.RAM,
                    Storage        = addComputer.Storage,
                    Stock          = addComputer.Stock,
                    Price          = addComputer.Price
                };
                db.Computers.Add(computer);
                var result = await db.SaveChangesAsync();

                if (result > 0)
                {
                    return(RedirectToAction("Index"));
                }
            }
            return(View("Error"));
        }
예제 #2
0
        public async Task <ActionResult> Edit(ViewModels.Computer edit)
        {
            if (ModelState.IsValid)
            {
                var computer = await db.Computers.Where(x => x.SKU == edit.SKU).SingleOrDefaultAsync();

                if (computer != null)
                {
                    computer.Title           = edit.Title;
                    computer.Description     = edit.Description;
                    computer.Stock           = edit.Stock;
                    computer.Price           = edit.Price;
                    db.Entry(computer).State = EntityState.Modified;
                    var result = await db.SaveChangesAsync();

                    if (result > 0)
                    {
                        return(RedirectToAction("Index"));
                    }
                }
            }
            return(View("HttpNotFound"));
        }