コード例 #1
0
        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));
        }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
 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));
 }
コード例 #4
0
        // 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));
            }
        }
コード例 #5
0
        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));
        }