问题描述
publicclassAccount{[Key]publicintID{get;set;}[Index(IsUnique=true)][Required(ErrorMessage="请输入账号")][Display(Name="账号")][MaxLength(50)]publicstringLoginName{get;set;}[Required][DataType(DataType.Password)][Display(Name="密码")][MaxLength(50)]publicstringPassword{get;set;}[Required][Display(Name="级别")][ForeignKey("UserLevel_ID")]publicvirtualUserLevelUserLevel{get;set;}}publicclassUserLevel{[Key]publicintID{get;set;}[Index(IsUnique=true)][Required(ErrorMessage="选择级别")][Display(Name="级别")][MaxLength(50)]publicstringUserLevelName{get;set;}}
是model,下面是修改代码publicActionResultEdit(intid,FormCollectioncollection){var_oldAccount=db.Accounts.Include("UserLevel").Where(t=>id.Equals(id)).FirstOrDefault();if(_oldAccount==null)ModelState.AddModelError("","用户不存在");else{if(!_oldAccount.Password.Equals(collection["Password"]))_oldAccount.Password=UserFunction.MD5Encrypt(collection["Password"]);_oldAccount.TrueName=collection["TrueName"];_oldAccount.Email=collection["Email"];intuserLevelID=int.Parse(collection["UserLevel.ID"]);varu=db.UserLevels.Where(t=>t.ID.Equals(userLevelID)).FirstOrDefault();_oldAccount.UserLevel=u;//_oldAccount.UserLevel=db.UserLevels.Find(userLevelID);db.Accounts.Attach(_oldAccount);db.UserLevels.Attach(_oldAccount.UserLevel);db.Entry(_oldAccount).State=EntityState.Modified;db.Entry(_oldAccount.UserLevel).State=EntityState.Modified;//db.Entry(_oldAccount.UserLevel).State=EntityState.Modified;//varentry=db.Entry<Account>(_oldAccount);//entry.Property(a=>a.UserLevel).IsModified=true;intflag=db.SaveChanges();if(flag>0)returnRedirectToAction("List");elseModelState.AddModelError("","修改失败!");}varUserLevels=db.UserLevels.Where(t=>t.ID!=1).OrderBy(t=>t.ID).Select(t=>newSelectListItem{Text=t.UserLevelName,Value=t.ID.ToString(),Selected=t.ID.Equals(_oldAccount.UserLevel.ID)});ViewBag.UserLevels=UserLevels;returnView(_oldAccount);}
我试了很多方法,就是不能修改Userlevel的值,其他都能修改,求高手指点下啊
解决方案
解决方案二:
没人???》
解决方案三:
跪求高手指点下
解决方案四:
顶!d====
解决方案五:
没细看。你if(!_oldAccount.Password.Equals(collection["Password"]))_oldAccount.Password=UserFunction.MD5Encrypt(collection["Password"]);_oldAccount.TrueName=collection["TrueName"];_oldAccount.Email=collection["Email"];intuserLevelID=int.Parse(collection["UserLevel.ID"]);确定不需要大括号包起来?collection["UserLevel.ID"]断点出来的值时多少?
解决方案六:
这种事情,你自己下个断点,或者跟踪一下sql语句就行了,有多难?