Exemplo n.º 1
0
 public ActionResult Edit([Bind(Include = "VolunteerId,FirstName,LastName,Username,Password,Address,PhoneNumber,EmailAddress,EmergencyContact,DriversLicenseOnFile,SSCardOnFile,EducationId")] Volunteer volunteer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(volunteer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.EducationId = new SelectList(db.Educations, "EducationId", "EducationLevel", volunteer.EducationId);
     return(View(volunteer));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Saves data to database
        /// </summary>
        public virtual void Update(T entity, bool isDeleted = false)
        {
            if (entity == null)
            {
                throw new ArgumentException("Cannot add a null entity.");
            }

            var entry = DataContext.Entry <T>(entity);

            if (entry.State == EntityState.Detached || entry.State == EntityState.Modified)
            {
                object pkey;

                if (entity.GetType().GetProperty("Id") != null)
                {
                    pkey = entity.GetType().GetProperty("Id").GetValue(entity);
                }
                else
                {
                    pkey = entity.GetType().GetProperty("ID").GetValue(entity);
                }

                var set            = DataContext.Set <T>();
                T   attachedEntity = _dbSet.Find(pkey); // You need to have access to key

                if (attachedEntity != null)
                {
                    var attachedEntry = DataContext.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(entity);
                }
                else
                {
                    entry.State = EntityState.Modified; // This should attach entity
                }
                //DataContext.SaveChanges();
            }
        }