public ActionResult Create(PersonModel model) { //MVC uses ModelState to track the correctness of the supplied Person "model." //If the model is valid, it means that MVC didn't detect any errors as defined //in the attributes in the Person class. if (ModelState.IsValid) { //Add the person to the DB. This queues them for insertion but doesn't //actually insert the value into the DB. For that, we need the next command. //model.CanWriteArticles = true; Db.People.Add(model); //tell the DB to save any queued changes. Db.SaveChanges(); //Once we're done, redirect to the Index action of HomeController. return RedirectToAction("Login"); } else { ModelState.AddModelError("", "One or more issues were found with your submission. Please try again."); } //If we got here, it means that the model's state is invalid. Simply return //to the create page and display any errors. return View("Login", model); }
public ActionResult NewComment(PersonModel model) { PersonModel person = Db.People.Where(p => p.Id == model.Id).FirstOrDefault(); if (person != null) { BlogPost bp = new BlogPost(); bp.comments.Add(model.newComment); person.blogPosts.Add(bp); model.newComment = ""; Db.SaveChanges(); return View("MyBlogs", new { model = person }); } else { return View("MyBlogs"); } }
public ActionResult Login(PersonModel model) { int uId = 0; bool found = false; Session["userid"] = null; Session["isadmin"] = false; Session["canwrite"] = false; foreach (PersonModel p in Db.People) { if (model.Email == p.Email && model.Password == p.Password) { uId = p.Id; Session["userid"] = p.Id; Session["name"] = p.FirstName + " " + p.LastName; Session["isadmin"] = p.isAdmin; Session["login"] = true; Session["canwrite"] = p.CanWriteArticles; Db.CurrentBlogger = p; found = true; //break; } } if (!found) { foreach (string key in ModelState.Keys) { ModelState[key].Errors.Clear(); } ModelState.AddModelError("", "Invalid User Name / Password"); return View("Login"); } Db.SaveChanges(); //return RedirectToAction("Login"); //return View("MyBlogs", new { model = db.CurrentBlogger }); return RedirectToAction("BlogIndex", "Blog"); }
public ActionResult NewBlog(PersonModel model) { PersonModel person = Db.People.Where(p => p.Id == model.Id).FirstOrDefault(); if (person != null) { BlogPost bp = new BlogPost(); bp.blog = model.newBlog; person.blogPosts.Add(bp); //db.Blogs.Add(bp); model.newBlog = ""; //Db.CurrentBlogger = person; } //person.blogPosts = db.Blogs.Where(b => b.bloggerId == person.Id).ToList(); Db.SaveChanges(); return View("MyBlogs", Db.CurrentBlogger); }
public ActionResult Edit(PersonModel model) { //again, check modelstate to make sure everything went okay if (ModelState.IsValid) { //Because the item already exists in the DB, we want to tell EF that //one of its models has been changed. We use this somewhat strange syntax to //accomplish this task. Db.Entry(model).State = System.Data.Entity.EntityState.Modified; //Again, the above command adds the request to a queue. To execute the queue, //we need to call SaveChanges() Db.SaveChanges(); //when complete, redirect to Index return RedirectToAction("Index"); } //Things must've went bad, so send back to the Create view. return View("Create", model); }