public ActionResult Create(AboutOwnerViewModel model)
        {
            Extensions.ExtensionMethod extension = new Extensions.ExtensionMethod();
            string uniqueFileName = extension.ProcessUploadFile(model, _hosting);

            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                var about = _mapper.Map <About>(model);
                about.CompanyPhoto = uniqueFileName;

                var isSuccess = _repo.Create(about);
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Something went wrong...");
                    return(View(model));
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ModelState.AddModelError("", "Something went wrong...");
                return(View());
            }
        }
Example #2
0
        //Extension method for About Create Method(1 photo).
        public string ProcessUploadFile(AboutOwnerViewModel model, IWebHostEnvironment _hosting)
        {
            string uniqueFileName = null;

            if (model.CompanyPhoto != null)
            {
                string uploadsFolder = Path.Combine(_hosting.WebRootPath, "companyImage");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.CompanyPhoto.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.CompanyPhoto.CopyTo(fileStream);
                }
            }

            return(uniqueFileName);
        }