Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("CodigoAgrotoxico,CodigoPraga")] AuxiliarAgrotoxico auxiliarAgrotoxico)
        {
            if (ModelState.IsValid)
            {
                _context.Add(auxiliarAgrotoxico);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CodigoAgrotoxico"] = new SelectList(_context.Agrotoxicos, "Codigo", "Nome", auxiliarAgrotoxico.CodigoAgrotoxico);
            ViewData["CodigoPraga"]      = new SelectList(_context.Pragas, "Codigo", "NomePopular", auxiliarAgrotoxico.CodigoPraga);
            return(View(auxiliarAgrotoxico));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(int CodigoAgrotoxico, int CodigoPraga, AuxiliarAgrotoxicoViewModel auxiliarAgrotoxicoViewModel, [Bind("CodigoAgrotoxico,CodigoPraga")] AuxiliarAgrotoxico auxiliarAgrotoxico)
        {
            if (CodigoAgrotoxico != auxiliarAgrotoxico.CodigoAgrotoxico || CodigoPraga != auxiliarAgrotoxico.CodigoPraga)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var agrotoxicos = await _context.Agrotoxicos
                                      .Include(a => a.ListaAuxiliarAgrotoxicos)
                                      .FirstOrDefaultAsync(a => a.Codigo == CodigoAgrotoxico);

                    var pragas = await _context.Pragas
                                 .Include(a => a.ListaAuxiliarAgrotoxicos)
                                 .FirstOrDefaultAsync(a => a.Codigo == CodigoPraga);

                    _context.AuxiliarAgrotoxicos.RemoveRange(agrotoxicos.ListaAuxiliarAgrotoxicos
                                                             .Where(a => a.CodigoAgrotoxico == CodigoAgrotoxico && a.CodigoPraga == CodigoPraga));

                    _context.AuxiliarAgrotoxicos.RemoveRange(pragas.ListaAuxiliarAgrotoxicos
                                                             .Where(a => a.CodigoAgrotoxico == CodigoAgrotoxico && a.CodigoPraga == CodigoPraga));

                    foreach (var item in _context.Agrotoxicos)
                    {
                        foreach (var item2 in _context.Pragas)
                        {
                            if (item.Codigo == auxiliarAgrotoxicoViewModel.SelecionadoKeysAgrotoxico)
                            {
                                auxiliarAgrotoxico.CodigoAgrotoxico = item.Codigo;
                                auxiliarAgrotoxico.Agrotoxico       = item;
                            }
                            if (item2.Codigo == auxiliarAgrotoxicoViewModel.SelecionadoKeysPraga)
                            {
                                auxiliarAgrotoxico.CodigoPraga = item2.Codigo;
                                auxiliarAgrotoxico.Praga       = item2;
                            }
                        }
                    }
                    _context.Add(auxiliarAgrotoxico);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuxiliarAgrotoxicoExists(auxiliarAgrotoxico.CodigoAgrotoxico, auxiliarAgrotoxico.CodigoPraga))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CodigoAgrotoxico"] = new SelectList(_context.Agrotoxicos, "Codigo", "Nome", auxiliarAgrotoxico.CodigoAgrotoxico);
            ViewData["CodigoPraga"]      = new SelectList(_context.Pragas, "Codigo", "NomePopular", auxiliarAgrotoxico.CodigoPraga);
            return(View(auxiliarAgrotoxico));
        }