Esempio n. 1
0
        public ActionResult New()
        {
            var bloodTypes = _context.BloodTypes.ToList();
            var viewModel  = new DonorFormViewModel
            {
                BloodTypes = bloodTypes
            };

            return(View("DonorForm", viewModel));
        }
Esempio n. 2
0
        public ActionResult New()
        {
            var Gender    = _context.Gender.ToList();
            var Bloodtype = _context.BloodType.ToList();

            var viewModel = new DonorFormViewModel
            {
                Gender = Gender,

                BloodTypes = Bloodtype
            };

            return(View("New", viewModel));
        }
Esempio n. 3
0
        public ActionResult Edit(int id)
        {
            var donor = _context.Donors.SingleOrDefault(d => d.Id == id);

            if (donor == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new DonorFormViewModel(donor)
            {
                BloodTypes = _context.BloodTypes.ToList()
            };

            return(View("DonorForm", viewModel));
        }
Esempio n. 4
0
        public ActionResult Save(Donor donor) //model binding
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new DonorFormViewModel(donor)
                {
                    BloodTypes = _context.BloodTypes.ToList()
                };

                return(View("DonorForm", viewModel));
            }

            if (donor.Id == 0)              //new donor
            {
                _context.Donors.Add(donor); //it's just in the memory =? we need to save them in the db
            }
            else //existing donor
            {
                var donorInDb = _context.Donors.Single(d => d.Id == donor.Id); //Single throws an exception if donor is not found. Not the case, bcs the action is called when posting donor form

                //TryUpdateModel(donorInDb);//updates all properties on donor (security bridges)
                //TryUpdateModel(donorInDb, "", new [] {"FirstName", "LastName"})

                donorInDb.FirstName   = donor.FirstName;
                donorInDb.LastName    = donor.LastName;
                donorInDb.BirthDate   = donor.BirthDate;
                donorInDb.BloodTypeId = donor.BloodTypeId;
                donorInDb.IsSubscribedToNewsletter = donor.IsSubscribedToNewsletter;

                //AutoMapper -> Maper.Map(donor, donorInDB)
                //UpdateDonorDto (data transform object) - another class only with attributes that can be updated
            }

            try
            {
                _context.SaveChanges(); //wraps the changes in a transaction.
            }
            catch (DbEntityValidationException e)
            {
                Console.WriteLine(e);
            }


            return(RedirectToAction("Index", "Donors"));
        }
Esempio n. 5
0
        public ActionResult Edit(int id)
        {
            var donors = _context.Donors.SingleOrDefault(c => c.id == id);

            if (donors == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new DonorFormViewModel
            {
                Gender     = _context.Gender.ToList(),
                Donor      = donors,
                BloodTypes = _context.BloodType.ToList()
            };

            return(View("New", viewModel));
        }
Esempio n. 6
0
        public ActionResult Create(DonorFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }

            var donor = new Donor
            {
                Name      = model.Name,
                Email     = model.Email,
                City      = model.City,
                BloodType = model.BloodType
            };

            _unitOfWork.Donors.Add(donor);
            _unitOfWork.Complete();

            return(RedirectToAction("Index", "Home"));
        }
Esempio n. 7
0
        public async Task <ActionResult> Create(DonorFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }

            var donor = new Donor
            {
                Name      = model.Name,
                Email     = model.Email,
                City      = model.City,
                BloodType = model.BloodType,
                Confirmed = false
            };

            try
            {
                _unitOfWork.Donors.Add(donor);
                _unitOfWork.Complete();

                var message = PrepareMessage(donor);
                if (message == null)
                {
                    ViewBag.errorMessage = "Oh noes. Something terrible happened.";
                    return(View("Error"));
                }

                await _emailService.SendAsync(message);

                ViewBag.Message = "We have sent you activation email. Please check you mailbox.";

                return(View("Info"));
            }
            catch
            {
                ViewBag.errorMessage = "Uh oh... Something went wrong.";
                return(View("Error"));
            }
        }
Esempio n. 8
0
        public ActionResult Create(Donors donor)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new DonorFormViewModel
                {
                    Donor      = donor,
                    BloodTypes = _context.BloodType.ToList(),
                    Gender     = _context.Gender.ToList()
                };

                return(View("New", viewModel));
            }

            if (donor.id == 0)
            {
                _context.Donors.Add(donor);
            }
            else
            {
                var DonorsInDb = _context.Donors.Single(c => c.id == donor.id);
                DonorsInDb.name         = donor.name;
                DonorsInDb.BloodTypeId  = donor.BloodTypeId;
                DonorsInDb.mobileNum    = donor.mobileNum;
                DonorsInDb.email        = donor.email;
                DonorsInDb.GenderId     = donor.GenderId;
                DonorsInDb.age          = donor.age;
                DonorsInDb.address      = donor.address;
                DonorsInDb.Message      = donor.Message;
                DonorsInDb.donationTime = DateTime.Now.ToString();
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Donor"));
        }
Esempio n. 9
0
        public ActionResult Create()
        {
            var model = new DonorFormViewModel();

            return(View(model));
        }