Exemplo n.º 1
0
        public ActionResult Edit(UsuarioView view)
        {
            if (ModelState.IsValid)
            {
                var db2     = new PersonalContext();
                var oldUser = db2.Usuarios.Find(view.Usuario.UserId);
                db2.Dispose();

                if (view.Foto != null)
                {
                    var pic = Uteis.UploadPhoto(view.Foto);
                    if (!string.IsNullOrEmpty(pic))
                    {
                        view.Usuario.Photo = string.Format("~/Content/Fotos/{0}", pic);
                    }
                }
                else
                {
                    view.Usuario.Photo = oldUser.Photo;
                }


                db.Entry(view.Usuario).State = EntityState.Modified;

                try
                {
                    if (oldUser != null && oldUser.UserName != view.Usuario.UserName)
                    {
                        Uteis.ChangeEmailUserAsp(oldUser.UserName, view.Usuario.UserName);
                    }
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                    return(View(view));
                }

                return(RedirectToAction("Index"));
            }
            return(View(view.Usuario));
        }
Exemplo n.º 2
0
        public ActionResult Create(UsuarioView view)
        {
            if (ModelState.IsValid)
            {
                db.Usuarios.Add(view.Usuario);

                try
                {
                    if (view.Foto != null)
                    {
                        var pic = Uteis.UploadPhoto(view.Foto);
                        if (!string.IsNullOrEmpty(pic))
                        {
                            view.Usuario.Photo = string.Format("~/Content/Fotos/{0}", pic);
                        }
                    }
                    db.SaveChanges();
                    Uteis.CreateUserASP(view.Usuario.UserName);

                    if (view.Usuario.Estudante)
                    {
                        Uteis.AddRoleToUser(view.Usuario.UserName, "Aluno");
                    }

                    if (view.Usuario.Professor)
                    {
                        Uteis.AddRoleToUser(view.Usuario.UserName, "Professor");
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }

            return(View(view));
        }