很多人在做修改操作的時(shí)候一般都用下面的這種
studentModel.Entry(model).State = EntityState.Modified;
當(dāng)我出現(xiàn)了另外的情況時(shí):
比如管理員管理用戶的數(shù)據(jù)時(shí),管理員不能獲取到用戶的密碼,那傳回的model里面的psw是為null
不說能不能改,能改你也把別人的密碼默認(rèn)改沒了。因?yàn)槟銢]有獲取到密碼。數(shù)據(jù)庫屬性不為NULL
所以就有了這么一個(gè)東西,TryUpdateModel
我是這么用的:
student stu = studentModel.student.Where(
p=>p.Student_number.Equals(model.Student_number)
).FirstOrDefault();//獲取學(xué)生數(shù)據(jù)。
? ?if (TryUpdateModel(stu, new string[]?
{ "Student_name", "Student_sex", "Student_age", "Student_dept", "Student_address" }
))//判定修改的屬性是否在 stu 的屬性里的可修改列表中(很拗口)
? ? ? ?{
? ? ? ? ? studentModel.SaveChanges();
? ? ? ? ? ?return RedirectToAction("Index");
? ? ? }
如果想要了解更多?http://www.jb51.net/article/72316.htm
https://www.cnblogs.com/superfeeling/p/4900072.html
對(duì)你有幫助的話請(qǐng)留下你的小腳印