public AccountController(IRepository<UserProfile> userProfile, IRepository<Usuario> user, IRepository<Cidade> city, IRepository<Estado> state, IRepository<Endereco> address)
 {
     _addressRepository = address;
     _userRepository = user;
     _cityRepository = city;
     _userProfileRepository = userProfile;
     _accountViewModel = new AccountViewModel();
     _stateRepository = state;
 }
        public ActionResult Register(AccountViewModel accountViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var user = _userProfileRepository.Get(w => w.UserName == accountViewModel.UserProfile.UserName);

                    if (null == user)
                    {

                        WebSecurity.CreateUserAndAccount(accountViewModel.UserProfile.UserName, accountViewModel.Password, new { UserName = accountViewModel.UserProfile.UserName });
                        WebSecurity.Login(accountViewModel.UserProfile.UserName, accountViewModel.Password, true);

                        accountViewModel.Users.UserId = WebSecurity.GetUserId(accountViewModel.UserProfile.UserName);
                        accountViewModel.Users.Endereco.CidadeID = int.Parse(accountViewModel.City);
                        //accountViewModel.Users.Endereco.EstadoID = short.Parse(accountViewModel.State);
                        accountViewModel.Users.Endereco.MostraLocalizacao = true;

                        _userRepository.Insert(accountViewModel.Users);
                        _addressRepository.Insert(accountViewModel.Users.Endereco);

                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }

                }
                catch (DbEntityValidationException dbEx)
                {
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        }
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(accountViewModel);
        }
        public ActionResult Edit(AccountViewModel acc)
        {
            var userId = Membership.GetUser().ProviderUserKey;

            if (userId != null)
            {
                if (ModelState.IsValid)
                {
                    int _userId = int.Parse(userId.ToString());

                    //acc.Users.UserId = _userRepository.Get(w => w.UserId == _userId).ID;
                    //acc.Users.UserId = _userId;
                    acc.UploadImage = ViewBag.FileName;
                    acc.Users.Endereco.ID = acc.Users.EnderecoId;
                    acc.Users.Endereco.CidadeID = int.Parse(acc.City);
                    //acc.Users.Endereco.EstadoID = short.Parse(acc.State);

                    try
                    {
                        _userRepository.Update(acc.Users);
                        _addressRepository.Update(acc.Users.Endereco);
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                            }
                        }
                    }
                    catch (Exception ex)
                    {

                        throw;
                    }
                }
            }
            return View(acc);
        }