Exemplo n.º 1
0
        public IActionResult Create([ModelBinder(binderType: typeof(SponsorModelBinder))] Sponsor sponsor)
        {
            List <Kid> kids = new List <Kid>();

            foreach (var vKid in sponsor.SponsoredKids)
            {
                var kid = _context.Kids.Find(vKid.Id);
                kids.Add(kid);
            }

            sponsor.SponsoredKids = kids;
            _context.Sponsors.Add(sponsor);
            _context.SaveChanges();

            bool isEmailSent = SendEmailToSponsor(sponsor);

            if (isEmailSent)
            {
                sponsor.LastConfirmationEmail = DateTime.Now;
                _context.Sponsors.Update(sponsor);
                _context.SaveChangesAsync();
            }

            ViewBag.SponsorName = sponsor.Name;
            ViewBag.Kids        = sponsor.SponsoredKids;
            ViewBag.IsEmailSent = isEmailSent;

            return(View("SubmissionResult"));
        }
        public IActionResult CreateKid(int id, string name, string arabicName, DateTime dateOfBirth, string description, IFormFile photo)
        {
            if (ModelState.IsValid)
            {
                if (photo != null)
                {
                    Kid kid = new Kid()
                    {
                        Id          = id,
                        Name        = name,
                        ArabicName  = arabicName,
                        DateOfBirth = dateOfBirth,
                        Description = description
                    };

                    SaveFile(photo, kid.Id);

                    _context.Kids.Add(kid);
                    _context.SaveChanges();
                    ViewBag.KidName = kid.Name;

                    return(View("SubmissionSuccessful"));
                }
            }
            return(View("SubmissionFailed"));
        }
Exemplo n.º 3
0
        public IActionResult Post(string values)
        {
            var model   = new Kid();
            var _values = JsonConvert.DeserializeObject <IDictionary>(values);

            PopulateModel(model, _values);

            if (!TryValidateModel(model))
            {
                return(BadRequest(GetFullErrorMessage(ModelState)));
            }

            var result = _context.Kids.Add(model);

            _context.SaveChanges();

            return(Json(result.Entity.Id));
        }