public async Task <IActionResult> Index(IEnumerable <FertilizerViewModel> fertilizers)
        {
            if (!ModelState.IsValid)
            {
                TempData["message"] = "Błąd: Wystąpił problem. Nie wprowadzono zmian";
                return(View(fertilizers));
            }

            var FertilizersToSave = fertilizers.Select(x => new Fertilizer
            {
                Id   = x.Id,
                Name = x.Name,
                N    = x.N,
                P    = x.P,
                K    = x.K,
                Ca   = x.Ca,
                Mg   = x.Mg,
                S    = x.S,
                Na   = x.Na,
            });


            foreach (var item in FertilizersToSave)
            {
                await _fertilizerRepository.Update(item);
            }

            var fertilizersToDelete = fertilizers.Where(x => x.Delete == true);

            foreach (var item in fertilizersToDelete)
            {
                var fertilizer = await _fertilizerRepository.GetById(item.Id);

                await _fertilizerRepository.Delete(fertilizer);
            }
            TempData["message"] = "Zmiany zapisano pomyślnie!";
            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Create(IList <AddTreatmentViewModel> model)
        {
            if (ManagedYearPlan == null)
            {
                return(ActionIfNotChoosedManagedYearPlan());
            }
            if (!ModelState.IsValid)
            {
                var kinds = await _treatmentKindRepository.GetAll();

                var fields = await _fieldRepository.GetByYearPlan(ManagedYearPlan);

                var fertilizers = await _fertilizerRepository.GetAll();

                var KindList       = new SelectList(kinds, "Id", "Name");
                var FieldList      = new SelectList(fields, "Id", "Name");
                var FertilizerList = new SelectList(fertilizers, "Id", "Name");

                ViewBag.Kinds       = KindList;
                ViewBag.Fields      = FieldList;
                ViewBag.Fertilizers = FertilizerList;
                return(View(model));
            }

            var treatments = model.Select((x) => new Treatment
            {
                Date          = x.Date,
                Notes         = x.Notes,
                DosePerHa     = x.DosePerHa,
                Composition   = x.SprayingAgents,
                ReasonForUse  = x.ReasonForUse,
                Field         = _fieldRepository.GetById(x.FieldId).Result,
                Fertilizer    = _fertilizerRepository.GetById(x.FertilizerId).Result,
                TreatmentKind = _treatmentKindRepository.GetById(x.TreatmentKindId).Result
            });
            await _treatmentRepository.AddRange(treatments);

            TempData["message"] = "Pomyślnie dodano nowe zabiegi";
            return(RedirectToAction("Index"));
        }