使用httpPost表单<edited>中的viewmodel仅更新一个字段

时间:2016-03-16 13:54:50

标签: c# asp.net-mvc asp.net-mvc-5 modelstate

此代码假设只更新表中的一个值,并保持所有其他值不变。

我怎么能找不到原因,db.SaveChanges不保存它。在调试器中我可以看到值已经改变,

这是我的代码

 if (model.rev.GBU == "Good")
                 {
                     //This section will do:---- 1> get subjectId -- 2> Update  only in the subjectid  good rank
                     //Update this subjectid   (int)good +1  data 
                    model.sub.SubjectId = R_getvelue.SubjectId;


                     //get the relevant value from the Db
                     var Good = R_getvelue.Good;
                     int iGoodRating = Convert.ToInt32(Good);
                     //Add 1 (one )to the value in the Db
                     iGoodRating++;
                     model.sub.Good = iGoodRating;
                     //Keep All the existing data in the other fields 
                     UpdateModel(model.sub);
                     // db.Entry(model.sub).State = EntityState.Modified; >removing all values biside "Good

                }

             await db.SaveChangesAsync();

            return RedirectToAction("index");
         }

        ViewBag.OccupationId = new SelectList(db.occupation, "OccupationId", "OccupationDecription", model.Occupation);
         return View(model);
     }

0 个答案:

没有答案