Пример #1
0
        public ActionResult AddPost(int id,Post post)
        {
            var blog = db.Blogs.FirstOrDefault(m => m.Id == id);
            if (ModelState.IsValid)
            {

                blog.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Posts", new { id = blog.Id });
            }

            return View(blog);
        }
Пример #2
0
        public ActionResult EditPost(Post post)
        {
            if (ModelState.IsValid)
            {
                db.Entry(post).State = EntityState.Modified;
                try
                {
                    db.SaveChanges();
                    //post = db.Posts.Include(m=>m.Blog).FirstOrDefault(m => m.Id == post.Id);
                    ////Reading original value of a property
                    //var orgBody = db.Entry(post).Property(m => m.Body).OriginalValue;
                    ////Reading the current changed value
                    //var changedBody = db.Entry(post).Property(m => m.Body).CurrentValue;
                    ////Check if the property is modified?
                    //var isBodyModified = db.Entry(post).Property(m => m.Body).IsModified;
                    ////Returns all properties original values as DbPropertyValues
                    //var allOriginalValues=db.Entry(post).OriginalValues;
                    ////Returns all properties current values as DbPropertyValues
                    //var allCurrentValues = db.Entry(post).CurrentValues;
                    ////Get values from db as DbPropertyValues
                    //var dbValues = db.Entry(post).GetDatabaseValues();
                    ////looping and displaying values
                    //foreach (var propName in dbValues.PropertyNames)
                    //{
                    //    var propValue = dbValues[propName]; //returns object
                    //    var propValue2 = dbValues.GetValue<string>(propName); //strongly typed

                    //}

                    ////setting new values
                    //dbValues.SetValues(post);

                    return RedirectToAction("Posts", new { id = post.Blog.Id });
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entry = ex.Entries.SingleOrDefault();
                    var dbPost = entry.GetDatabaseValues().ToObject() as Post;
                    if (!dbPost.Title.Equals(post.Title))
                    {
                        ModelState.AddModelError("Title",string.Format( "Current Value : {0}", dbPost.Title));
                    }

                    if (!dbPost.Body.Equals(post.Body))
                    {
                        ModelState.AddModelError("Body",string.Format( "Current Value : {0}", dbPost.Body));
                    }

                    return View(post);
                }
                catch (Exception)
                {

                    throw;
                }
            }

            return View(post);
        }