示例#1
0
        // GET: PoliticParties/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (await _electionService.VerifyElectionOpenAsync())
            {
                return(RedirectToAction("Index"));
            }
            if (id == null)
            {
                return(NotFound());
            }

            var politicParty = await _politicPartyService.GetPoliticParties().FirstOrDefaultAsync(pp => pp.Id == id);

            var politicPartyCreateViewModel = new PoliticPartyCreateViewModel()
            {
                Id          = politicParty.Id,
                Name        = politicParty.Name,
                Description = politicParty.Description,
                Photo       = politicParty.PartyLogoPath,
                IsActive    = politicParty.IsActive
            };


            if (politicParty == null)
            {
                return(NotFound());
            }
            return(View(politicPartyCreateViewModel));
        }
示例#2
0
        private string ProcessUploadedFile(PoliticPartyCreateViewModel model)
        {
            string uniqueFileName = null;

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

            return(uniqueFileName);
        }
示例#3
0
        public async Task <IActionResult> Create(PoliticPartyCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedFile(model);
                if (uniqueFileName == null)
                {
                    uniqueFileName = "PartidoNull.png";
                }
                PoliticParty politicParty = new PoliticParty
                {
                    Name          = model.Name,
                    Description   = model.Description,
                    PartyLogoPath = uniqueFileName,
                    IsActive      = model.IsActive
                };

                _politicPartyService.InsertPoliticParty(politicParty);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
示例#4
0
        public async Task <IActionResult> Edit(int id, /* [Bind("Id,Name,Description,PartyLogoPath,IsActive")]*/ PoliticPartyCreateViewModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedFile(model);
                if (uniqueFileName == null)
                {
                    uniqueFileName = model.Photo;
                }

                var politicParty = new PoliticParty()
                {
                    Id            = model.Id,
                    Name          = model.Name,
                    Description   = model.Description,
                    PartyLogoPath = uniqueFileName,
                    IsActive      = model.IsActive
                };
                try
                {
                    _politicPartyService.UdatePoliticParty(politicParty);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PoliticPartyExists(politicParty.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }