Пример #1
0
        public ActionResult Create(Post post)
        {
            var CurrentUser = db.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);

            post.Poster = CurrentUser;
            post.PostedOn = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            var errors = ModelState.Select(x => x.Value.Errors)
                          .Where(y => y.Count > 0)
                          .ToList();
            return View(post);
        }
Пример #2
0
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {
                Post edited = db.Posts.Find(post.Id);
                edited.Text = post.Text;

                db.Entry(edited).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(post);
        }