public async Task <IActionResult> Create(UnionMemberViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var model   = new UnionMember(viewModel);
                var webRoot = _env.WebRootPath;
                if (viewModel.Picture != null)
                {
                    Tuple <bool, string> result = await _spiikService.SaveFile(viewModel.Picture, "images/unionmembers", viewModel.Name);

                    if (result.Item1)                                 // Success saving
                    {
                        if (!string.IsNullOrWhiteSpace(result.Item2)) // Spara endast om en bild har blivit uppladdad.
                        {
                            model.PictureSrc = result.Item2;
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Picture", result.Item2);
                        return(View());
                    }
                }
                await _db.AddAsync(model);

                await _db.SaveChangesAsync();

                TempData["Message"] = "Medlem skapad!";
                return(RedirectToAction("Index"));
            }
            return(View());
        }
Beispiel #2
0
 public UnionMember(UnionMemberViewModel vm)
 {
     Name       = vm.Name;
     Title      = vm.Title;
     UnionTypes = vm.UnionTypes;
     Email      = vm.Email;
     Phone      = vm.Phone;
     Quote      = vm.Quote;
     PictureSrc = vm.PictureSrc;
 }
        public async Task <IActionResult> Edit(int id)
        {
            var model = await _db.UnionMembers.FindAsync(id);

            if (model == null)
            {
                return(RedirectToAction("Index"));
            }

            var viewModel = new UnionMemberViewModel(model);

            return(View(viewModel));
        }
        [RequestFormSizeLimit(5000000)]         // Max image size 5 MB
        public async Task <IActionResult> Edit(UnionMemberViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var model = await _db.UnionMembers.FindAsync(viewModel.Id);

                if (model == null)
                {
                    return(RedirectToAction("Index"));
                }

                model.Name       = viewModel.Name;
                model.Title      = viewModel.Title;
                model.UnionTypes = viewModel.UnionTypes;
                model.Email      = viewModel.Email;
                model.Phone      = viewModel.Phone;
                model.Quote      = viewModel.Quote;

                if (viewModel.Picture?.Length > 0)
                {
                    Tuple <bool, string> result = await _spiikService.SaveFile(viewModel.Picture, "images/unionmembers", viewModel.Name);

                    if (result.Item1)                                 // Success saving
                    {
                        if (!string.IsNullOrWhiteSpace(result.Item2)) // Spara endast om en bild har blivit uppladdad.
                        {
                            model.PictureSrc = result.Item2;
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Picture", result.Item2);
                        return(View(viewModel));
                    }
                }

                await _db.SaveChangesAsync();

                TempData["Message"] = "Medlem uppdaterad!";
                return(RedirectToAction("Index"));
            }
            return(View());
        }