protected void btnInsertClick(object sender, EventArgs e) { using (_db) { var item = new HPPortal.Data.Models.User(); if (IsNew) { item = CommitToItem(item); if (ModelState.IsValid) { _db.Users.Add(item); _db.SaveChanges(); Response.Redirect("Default"); } } else { item = CommitToItem(User); if (ModelState.IsValid) { _db.Entry(item).State = EntityState.Modified; _db.SaveChanges(); Response.Redirect("../Default"); } } } }
// This is the Insert method to insert the entered User item // USAGE: <asp:FormView InsertMethod="InsertItem"> public void InsertItem() { using (_db) { var item = new HPPortal.Data.Models.User(); TryUpdateModel(item); item.Active = true; SaveCiyData(item); if (ModelState.IsValid) { // Save changes _db.Users.Add(item); _db.SaveChanges(); Response.Redirect("Default"); } } }