Unable to do changes in MVC
-
My Edit page won't do any changes to the database. I'm sure that my code is right. However it won't make any changes! I'm using Entity framework code first.
HttpPost]
public ActionResult Edit(User user, int? Id)
{var userInfo = db.Users.SingleOrDefault(s => s.UserId == Id); if (userInfo != null) { db.SaveChanges(); } return View(); }
}
-
My Edit page won't do any changes to the database. I'm sure that my code is right. However it won't make any changes! I'm using Entity framework code first.
HttpPost]
public ActionResult Edit(User user, int? Id)
{var userInfo = db.Users.SingleOrDefault(s => s.UserId == Id); if (userInfo != null) { db.SaveChanges(); } return View(); }
}
Member 11929022 wrote:
I'm sure that my code is right
You sure about that? The biggest problem I see is that you go and get a user object from the database using its ID to look it up. Then you "SaveChanges" to the database, but you haven't made any! The controller apparently gets a User object from the caller, but you never use it in the method at all.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
My Edit page won't do any changes to the database. I'm sure that my code is right. However it won't make any changes! I'm using Entity framework code first.
HttpPost]
public ActionResult Edit(User user, int? Id)
{var userInfo = db.Users.SingleOrDefault(s => s.UserId == Id); if (userInfo != null) { db.SaveChanges(); } return View(); }
}
Are you using the Scaffholding entity framework techniques like create update delete and read operations which will actually make changes to the database??.. please apply those methods instead of db.SaveChanges(); as those are mainly integrated to perform CRUD operations on database and more over in your present code you are just reading the values from database and not updating or inserting anything and how you expect to commit changes to database bro? Think about it ok.