Any reason this LINQ to SQL update request is not working.,

Somehow this update code does not work:

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 used above (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:

Please note that everything works fine during debugging (with no exceptions), but when it redirects back to the index, the data was not updated when changes were made from the update.

+3
source share
3 answers

I just changed userrepository to the following:

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 () inline method and it worked.

Maybe it was a red herring, and it was just a cache problem ..

+1
source

, UserInfo, ?

, GetUser, , , .

GetUser , , .

afaik -

public void GetUser(int id_, out UserInfo user_)
{
    user_ = db.UserInfos.SingleOrDefault(d => d.Id == id_);
}

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();
}
0

Did you turn off object tracking?

0
source

Source: https://habr.com/ru/post/1711459/


All Articles