代码之家  ›  专栏  ›  技术社区  ›  Ehsan Akbar

在MVC中更新特定数量的列

  •  -1
  • Ehsan Akbar  · 技术社区  · 12 年前

    我有一个编辑操作来编辑我的 议论 型号:

    public partial class Comment
        {
            [DisplayName("شناسه نظر")]
            public int Id { get; set; }
            [Required(ErrorMessage = "متن نظر را وارد کنید")]
            [DisplayName("متن نظر")]
            public string CommentText { get; set; }
            [DisplayName("تعداد پسندیدن ")]
            public long LikeCount { get; set; }
            [DisplayName("تعداد نپسندیدن")]
            public long DisLikeCount { get; set; }
            [DisplayName("تاریخ انتشار ")]
            public System.DateTime PublishDate { get; set; }
            [DisplayName("وضعیت نمایش ")]
            public string Visible { get; set; }
            [DisplayName("نام کاربری ")]
            public string AutherUserName { get; set; }
            [DisplayName("شناسه نظراصلی")]
            public Nullable<int> CommentFKId { get; set; }
            [DisplayName("شناسه کاربر")]
            public Nullable<int> StudentId { get; set; }
            [DisplayName("شناسه محتوا ")]
            public Nullable<int> ContentId { get; set; }
    
       public virtual ICollection<Comment> Comments { get; set; }
            public virtual Comment Comment1 { get; set; }
            public virtual Student Student { get; set; }
            public virtual Content Content { get; set; }
        }
    

    我的模型中有很多列,但在编辑操作中,我只需要编辑其中的一些列。

    因此,我的Postback编辑操作如下:

       [HttpPost]
            public ActionResult Edit(Comment comment, FormCollection form)
            {
                //comment.AutherUserName = "admin";
                //comment.LikeCount = 0;
    
                if (ModelState.IsValid)
                {
                    TryUpdateModel(comment, new string[] {"CommentText", "Visible"});
                    obj.Update(comment);
                    obj.Save();
                }
    
              return RedirectToAction("Index", "Comment", new { contentID = form["ContentId"].ToString() });
            }
    

    如你所见,我想更新 注释文本 看得见的 。但此代码不会更新我的模型。它不会返回任何错误

    这是我的 使现代化 方法:

      public void Update(Comment comment)
            {
                _dbcontext.Entry(comment).State = EntityState.Modified;
            }
    

    我的保存功能:

     public void Save()
            {
                _dbcontext.SaveChanges();
    
            }
    

    顺致敬意,

    2 回复  |  直到 12 年前
        1
  •  1
  •   Ehsan Sajjad    12 年前

    这样做:

    public void Update(Comment comment)
            {
                _dbcontext.Entry(comment).State = EntityState.Modified;
                _dbContext.SaveChanges();
            }
    

    您正在更新对象,但不保存它以反映数据库中的对象更改。

        2
  •  0
  •   Ehsan Akbar    12 年前

    最后我用这个方法:

    [HttpPost]
            public ActionResult Edit(Comment comment, FormCollection form)
            {
    
                Comment temp = obj.FindCommentsById(comment.Id);
                temp.CommentText = comment.CommentText;
                temp.Visible = comment.Visible;
                if (ModelState.IsValid)
                {
                    //TryUpdateModel(comment, new string[] {"CommentText", "Visible"});
                    obj.Update(temp);
                    obj.Save();
                }
    
              return RedirectToAction("Index", "Comment", new { contentID = form["ContentId"].ToString() });
            }
    

    首先编辑所需的列,然后将记录的id传递给 查找注释ById 并将返回实体的值更改为新的值。

    推荐文章