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 AspnetNoteDBContext()) { db.Notes.Add(model); if (db.SaveChanges() > 0) { return(Redirect("Index")); } //db.SaveChanges(); //Commit } ModelState.AddModelError(string.Empty, "게시물을 저장할 수 없습니다."); } return(View(model)); }
[HttpPost] //post방식으로 전송했을 때의 메서드 오버로딩 public IActionResult Register(User model) { if (ModelState.IsValid) //null허용x인 필수값들 모두 받았는지? { using (var db = new AspnetNoteDBContext()) { db.Users.Add(model); db.SaveChanges(); } return(RedirectToAction("index", "Home")); //Home컨트롤러의 index액션으로 넘김 (view) } return(View()); }
public IActionResult Register(User model) { if (ModelState.IsValid) { using (var db = new AspnetNoteDBContext()) { db.Users.Add(model); db.SaveChanges(); } return(RedirectToAction("Index", "Home")); } return(View(model)); }
public IActionResult Register(User model) { if (ModelState.IsValid) { // Java try(SqlSession){} catch(){} // C# using (var db = new AspnetNoteDBContext()) { db.Users.Add(model); db.SaveChanges(); } //HomeController의 Index View로 넘긴다. return(RedirectToAction("Index", "Home")); } return(View()); }
public IActionResult Add(Note model) { if (ModelState.IsValid) { using (var db = new AspnetNoteDBContext()) { db.Notes.Add(model); if (db.SaveChanges() > 0) { return(Redirect("index")); // 동일한 컨드롤일경우 } } ModelState.AddModelError(string.Empty, "게시물을 저장할 수 없습니다."); } return(View()); }