public ActionResult Create(User user, HttpPostedFileBase Avatar) { if (ModelState.IsValid) { if (Avatar != null) { string name = System.IO.Path.GetFileName(Avatar.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/UserAvatar/"), name); Avatar.SaveAs(path); user.Avatar = Avatar.FileName; } else { user.Avatar = "default.png"; } var result = db.Users.Where(x => x.Username == user.Username && x.Active == true).Count(); if (result > 0) { ModelState.AddModelError("", "Tên tài khoản đã tồn tại"); } else { db.Users.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } } return View(user); }
public ActionResult Create(Advert Adverts, HttpPostedFileBase Images1, HttpPostedFileBase Images2, HttpPostedFileBase Images3) { HttpContext.Server.ScriptTimeout = 1000; if (ModelState.IsValid) { User user = new User(); user = dbMuaChung.Users.Where(x => x.Username == User.Identity.Name).FirstOrDefault(); Adverts.UserId = user.UserId; Adverts.Views = 123; Adverts.Created = DateTime.Now; if (Images1 != null) { string name = System.IO.Path.GetFileName(Images1.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Adverts/"), name); Images1.SaveAs(path); Adverts.Images1 = Images1.FileName; } if (Images2 != null) { string name = System.IO.Path.GetFileName(Images2.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Adverts/"), name); Images2.SaveAs(path); Adverts.Images2 = Images2.FileName; } if (Images3 != null) { string name = System.IO.Path.GetFileName(Images3.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/Adverts/"), name); Images3.SaveAs(path); Adverts.Images3 = Images3.FileName; } var AdvertId = dbMuaChung.Adverts.Where(p => p.AdvertId == Adverts.AdvertId).Count(); if (AdvertId > 0) { ModelState.AddModelError("", "* Adverts Id Userd"); } else { dbMuaChung.Adverts.Add(Adverts); dbMuaChung.SaveChanges(); return RedirectToAction("Index"); } } return View(Adverts); }
public ActionResult GetAvatar(string id = "", string view = "") { User user = new User(); if (String.IsNullOrEmpty(id)) id = User.Identity.Name; //user = null thì lấy thông tin của user đang đăng nhập if (id != null) { user = context.Users.Where(x => x.Username.Equals(id)).FirstOrDefault(); } else { int ids = int.Parse(id); user = context.Users.Where(x => x.UserId == ids).FirstOrDefault(); } return PartialView(view, user); }
public ActionResult Edit(User user, HttpPostedFileBase avatar) { if (ModelState.IsValid) { if (avatar != null) { string name = System.IO.Path.GetFileName(avatar.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/UserAvatar"), name); avatar.SaveAs(path); user.Avatar = avatar.FileName; } db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(user); }
//Hàm Trả về tên người dùng hiện tại và layout người dùng public ActionResult GetUserActive(string view = "") { User user = new User(); user = (User)Session["S_User"]; var model = context.Users.Where(x => x.Username.Equals(user.Username)).FirstOrDefault(); return PartialView(view, model); }
public ActionResult Login(User user) { if (CheckValid(user.Username, user.Password)) { FormsAuthentication.SetAuthCookie(user.Username, user.RememberMe); Session.Add("S_User", user); ViewBag.active = user.Username; return RedirectToAction("Index", "MuaChungManage"); } else { ModelState.AddModelError("", "Mat khau khong chinh xac"); } return View(user); }