Exemplo n.º 1
0
        public async Task <ActionResult> TestimonyEdit([FromForm] TestimonialCreateViewModel testimonyVM)
        {
            if (ModelState.IsValid)
            {
                var uploadedImage = "";
                var editTestimony = _mapper.Map <Testimonial>(testimonyVM);
                if (testimonyVM != null)
                {
                    uploadedImage = await ProcessPhoto(testimonyVM.TestimonyPhoto);
                }
                try
                {
                    if (!string.IsNullOrWhiteSpace(uploadedImage))
                    {
                        editTestimony.ImgUrl = uploadedImage;
                    }


                    await _testimonyRepo.UpdateTestimony(editTestimony);

                    TempData["Alert"] = "Testimonial Edited Successfully";
                    return(RedirectToAction(nameof(TestimonyIndex)));
                }
                catch (Exception ex)
                {
                    ViewBag.Error = "Unable to add testimonial, please try again or contact administrator";
                    return(View());
                }
            }
            ViewBag.Error = "Please correct the error(s) in Form";
            return(View());
        }
Exemplo n.º 2
0
        public async Task <ActionResult> TestimonyCreate([FromForm] TestimonialCreateViewModel testimonyVM)
        {
            if (ModelState.IsValid)
            {
                var uploadedImage = "";
                if (testimonyVM.TestimonyPhoto != null)
                {
                    uploadedImage = await ProcessPhoto(testimonyVM.TestimonyPhoto);
                }

                try
                {
                    var testimony = _mapper.Map <Testimonial>(testimonyVM);
                    testimony.CreatedAt = DateTime.Now;
                    testimony.ImgUrl    = uploadedImage;

                    if (await _testimonyRepo.AddTestimony(testimony))
                    {
                        TempData["Alert"] = "Testimonial Added Successfully";
                        return(RedirectToAction(nameof(TestimonyIndex)));
                    }

                    ViewBag.Error = "Unable to edit testimonial, please try again or contact administrator";
                    return(View());
                }
                catch (Exception ex)
                {
                    ViewBag.Error = "Unable to add testimonial, please try again or contact administrator";
                    return(View());
                }
            }
            ViewBag.Error = "Please correct the error(s) in Form";
            return(View(testimonyVM));
        }