private bool Save(Resources row) { try { row.ModifiedDate = DateTime.Now; row.ModifiedBy = User.Identity.Name; //If it's a new record, just attach it to the DataContext if (row.Id == 0) { swdb.Resources.Add(row); } else { // We're updating an existing record, but it's not attached to // this data context, so attach it and detect the changes swdb.Resources.Attach(row); swdb.Entry(row).State = EntityState.Modified; //ObjectStateEntry objectStateEntry = db.ObjectStateManager.GetObjectStateEntry(row); //foreach (string str in from fm in objectStateEntry.CurrentValues.DataRecordInfo.FieldMetadata select fm.FieldType.Name) //{ // objectStateEntry.SetModifiedProperty(str); //} } swdb.SaveChanges(); return(true); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); TempData["message"] = ex.Message; return(false); } }
public ActionResult Edit(Resources row) { if (ModelState.IsValid) { if (Save(row)) { TempData["message"] = row.Name + " has been saved."; } return(RedirectToAction("Index")); } else // Validation error, so redisplay same view { var model = new ResourceEditModel(); model.Row = row; return(View(model)); } }