Somehow this update code is not working:
不知何故,此更新代码无效:
Here is my Controller code:
这是我的控制器代码:
private UserRepository repo = new UserRepository();
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, UserInfo user_)
{
try
{
repo.UpdateUser(user_);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Here is the repo code that is used above (UserRepository)
这是上面使用的repo代码(UserRepository)
private UsersDataContext db = new UsersDataContext();
public void UpdateUser(UserInfo user_)
{
UserInfo origUser = GetUser(user_.Id);
origUser.First = user_.First;
origUser.Last = user_.Last;
origUser.City = user_.City;
origUser.Country = user_.Country;
origUser.State = user_.State;
origUser.Street_Address = user_.Street_Address;
db.SubmitChanges();
}
public UserInfo GetUser(int id_)
{
return db.UserInfos.SingleOrDefault(d => d.Id == id_);
}
EDIT:
Note that when debugging everything is running fine (no exceptions) but when it redirects back to Index the data has not been updated when the changes from the update.
请注意,在调试时,一切运行正常(没有例外),但当它重定向回Index时,数据在更新时发生更改时尚未更新。
3 个解决方案
#1
i just changed the userrepository to the following:
我刚刚将userrepository更改为以下内容:
private UsersDataContext db = new UsersDataContext();
public void UpdateUser(UserInfo user_)
{
UserInfo origUser = db.UserInfos.SingleOrDefault(d => d.Id == id_);
origUser.First = user_.First;
origUser.Last = user_.Last;
origUser.City = user_.City;
origUser.Country = user_.Country;
origUser.State = user_.State;
origUser.Street_Address = user_.Street_Address;
db.SubmitChanges();
}
so all i did was move the GetUser() method inline and it worked.
所以我所做的就是内联移动GetUser()方法并且它有效。
It might have been a red herring and it was just a caching issue . .
它可能是一个红色的鲱鱼,它只是一个缓存问题。 。
#2
You don't mention how you've defined UserInfo, is it a struct or a class?
你没有提到你如何定义UserInfo,它是一个结构还是一个类?
If it's a struct, returning it from GetUser will create a new object and thus you will not update the database object, only a local copy of it.
如果它是一个结构,从GetUser返回它将创建一个新对象,因此您不会更新数据库对象,只更新它的本地副本。
Moving the GetUser inline avoid this temp copy creation and that's likely why it's working.
内联移动GetUser可以避免创建临时副本,这可能就是它的工作原因。
afaik you could do something like
afaik你可以做点什么
public void GetUser(int id_, out UserInfo user_)
{
user_ = db.UserInfos.SingleOrDefault(d => d.Id == id_);
}
You would then call it like this
然后你会这样称呼它
public void UpdateUser(UserInfo user_)
{
UserInfo origUser;
GetUser(user_.Id, out origUser);
origUser.First = user_.First;
origUser.Last = user_.Last;
origUser.City = user_.City;
origUser.Country = user_.Country;
origUser.State = user_.State;
origUser.Street_Address = user_.Street_Address;
db.SubmitChanges();
}
#3
Maybe you disabled Object Tracking?
也许您禁用了对象跟踪?
#1
i just changed the userrepository to the following:
我刚刚将userrepository更改为以下内容:
private UsersDataContext db = new UsersDataContext();
public void UpdateUser(UserInfo user_)
{
UserInfo origUser = db.UserInfos.SingleOrDefault(d => d.Id == id_);
origUser.First = user_.First;
origUser.Last = user_.Last;
origUser.City = user_.City;
origUser.Country = user_.Country;
origUser.State = user_.State;
origUser.Street_Address = user_.Street_Address;
db.SubmitChanges();
}
so all i did was move the GetUser() method inline and it worked.
所以我所做的就是内联移动GetUser()方法并且它有效。
It might have been a red herring and it was just a caching issue . .
它可能是一个红色的鲱鱼,它只是一个缓存问题。 。
#2
You don't mention how you've defined UserInfo, is it a struct or a class?
你没有提到你如何定义UserInfo,它是一个结构还是一个类?
If it's a struct, returning it from GetUser will create a new object and thus you will not update the database object, only a local copy of it.
如果它是一个结构,从GetUser返回它将创建一个新对象,因此您不会更新数据库对象,只更新它的本地副本。
Moving the GetUser inline avoid this temp copy creation and that's likely why it's working.
内联移动GetUser可以避免创建临时副本,这可能就是它的工作原因。
afaik you could do something like
afaik你可以做点什么
public void GetUser(int id_, out UserInfo user_)
{
user_ = db.UserInfos.SingleOrDefault(d => d.Id == id_);
}
You would then call it like this
然后你会这样称呼它
public void UpdateUser(UserInfo user_)
{
UserInfo origUser;
GetUser(user_.Id, out origUser);
origUser.First = user_.First;
origUser.Last = user_.Last;
origUser.City = user_.City;
origUser.Country = user_.Country;
origUser.State = user_.State;
origUser.Street_Address = user_.Street_Address;
db.SubmitChanges();
}
#3
Maybe you disabled Object Tracking?
也许您禁用了对象跟踪?