public IActionResult Add(Note model) { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { return(RedirectToAction("Login", "Account")); } model.UserNo = int.Parse(HttpContext.Session.GetInt32("USER_LOGIN_KEY").ToString()); if (ModelState.IsValid) { using (var db = new AspNetNoteContext()) { db.Notes.Add(model); var result = db.SaveChanges(); //Commit if (result > 0) { return(Redirect("Index")); } } ModelState.AddModelError(string.Empty, "Cannot add the content"); } return(View(model)); }
public IActionResult Detail(int noteNo) { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { return(RedirectToAction("Login", "Account")); } using (var db = new AspNetNoteContext()) { var note = db.Notes.FirstOrDefault(n => n.NoteNo.Equals(noteNo)); return(View(note)); } }
public IActionResult Register(User model) { if (ModelState.IsValid) { using (var db = new AspNetNoteContext()) { db.Users.Add(model); //upload data to memory db.SaveChanges(); //save data to sql table } return(RedirectToAction("Index", "Home")); } return(View(model)); }
// GET: /<controller>/ public IActionResult Index() { if (HttpContext.Session.GetInt32("USER_LOGIN_KEY") == null) { return(RedirectToAction("Login", "Account")); } using (var db = new AspNetNoteContext()) { var list = db.Notes.ToList(); return(View(list)); } }
public IActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { using (var db = new AspNetNoteContext()) { var user = db.Users.FirstOrDefault(u => u.UserId.Equals(model.UserId) && u.UserPassword.Equals(model.UserPassword)); if (user != null) { HttpContext.Session.SetInt32("USER_LOGIN_KEY", user.UserNo); return(RedirectToAction("LoginSuccess", "Home")); } } ModelState.AddModelError(string.Empty, "User ID or Password is not correct"); } return(View(model)); }