Exemplo n.º 1
0
 public ActionResult EditAccount(UserViewModel user, int[] uploadedfile)
 {
     if (ModelState.IsValid)
     {
         var applicationUser = UserManager.Users.First(u => u.UserName == User.Identity.Name);
         applicationUser = AutoMapper.Mapper.Map<UserViewModel, ApplicationUser>(user, applicationUser);
         if (uploadedfile != null && uploadedfile.Length > 0)
         {
             File newFile = _fileService.getFile(uploadedfile[uploadedfile.Length-1]);
             if (newFile != null)
             {
                 if (applicationUser.UploadImage != null)
                 {
                     File oldFile = _fileService.getFile(applicationUser.UploadImage.FileId);
                     oldFile.FileName = newFile.FileName;
                     oldFile.Content = newFile.Content;
                     oldFile.ContentType = newFile.ContentType;
                     _fileService.saveFile();
                     if(oldFile.FileId != newFile.FileId)
                     {
                         _fileService.deleteFile(newFile);
                     }                            
                 }
                 else
                 {
                     applicationUser.FileId = newFile.FileId;
                 }
             }                                               
         }
         IdentityResult result = UserManager.Update(applicationUser);        
         if(result.Succeeded)       
             return RedirectToAction("MyInfo");
         foreach (var error in result.Errors)
         {
             ModelState.AddModelError("", error);
         }
     }
     ViewBag.CountryId = new SelectList(_categoryService.GetCategories(), "Code", "CodeName", user.CountryId);
     if (uploadedfile != null)
     {
         File file = _fileService.getFile(uploadedfile[uploadedfile.Length - 1]);
         if (file != null)
             user.UploadImage = file;
     }
     return View(user);
 }
Exemplo n.º 2
0
        public ActionResult Edit(UserViewModel model, int[] uploadedfile)
        {
            if (ModelState.IsValid)
            {
                var applicationUser = UserManager.FindByName(model.Email);
                applicationUser = Mapper.Map<UserViewModel, ApplicationUser>(model, applicationUser);

                if (uploadedfile != null && uploadedfile.Length > 0)
                {
                    File newFile = _fileService.getFile(uploadedfile[uploadedfile.Length - 1]);
                     if (newFile != null)
                    {
                        if (applicationUser.UploadImage != null)
                        {
                            File oldFile = _fileService.getFile(applicationUser.UploadImage.FileId);
                            oldFile.FileName = newFile.FileName;
                            oldFile.Content = newFile.Content;
                            oldFile.ContentType = newFile.ContentType;
                            _fileService.saveFile();
                            if(newFile.FileId != oldFile.FileId)
                            {
                                _fileService.deleteFile(newFile);
                            }

                        }
                        else
                        {
                            applicationUser.FileId = newFile.FileId;
                        }
                    }
                }
                applicationUser.Roles.Clear();
                var result = UserManager.Update(applicationUser);
                if (result.Succeeded)
                {
                    if (model.IsAdmin)
                    {
                        result = UserManager.AddToRole(applicationUser.Id, MemberRoles.Admin.ToString());
                    }
                    else
                    {
                        result = UserManager.AddToRole(applicationUser.Id, MemberRoles.Contributor.ToString());
                    }
                    if (result.Succeeded)
                    {
                        TempData["ReloadData"] = true;
                        return RedirectToAction("Index");
                    }

                }
                else
                {
                    AddErrors(result);
                }
                if (applicationUser.FileId != null)
                {
                    ViewBag.FileName = _fileService.getFile(applicationUser.FileId.Value).FileName;
                }
            }
            ViewBag.CountryId = new SelectList(categoryService.GetCountries(), "Code", "CodeName", model.CountryId);
            return View(model);
        }