public T Update(T entity) { var result = dbContext.Set <T>().Attach(entity); dbContext.Entry(entity).State = System.Data.Entity.EntityState.Modified; //xem lại dbContext.SaveChanges(); return(result); }
public ActionResult Edit([Bind(Include = "UserId,UserName,Password,Gender,Email,SecurityQuestion,SecurityAnswer,QQ,Tel,Address,PostCode,RegTime,LastLoginTime")] User user) { if (ModelState.IsValid) { db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public ActionResult Edit([Bind(Include = "CategoryId,CategoryName")] Category category) { if (ModelState.IsValid) { db.Entry(category).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public ActionResult Edit([Bind(Include = "Id,Title,Producer,Date,Category,DEscription")] News news) { if (ModelState.IsValid) { db.Entry(news).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(news)); }
public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Text")] NewsViewModel newsViewModel) { if (ModelState.IsValid) { db.Entry(newsViewModel).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(newsViewModel)); }
public ActionResult Edit(Article article) //修改的时候提交的数据时一个类 { if (ModelState.IsValid) { db.Entry(article).State = EntityState.Modified; // 修改 db.SaveChanges(); //保存 return(RedirectToAction("index")); } return(View("index")); }
public ActionResult Edit([Bind(Include = "Id,TimeStamp,Text, RowVersion")] NewsItem newsItem) { if (ModelState.IsValid) { try { db.Entry(newsItem).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } catch (DbUpdateConcurrencyException ex) { ModelState.AddModelError(string.Empty, "Unable to save changes. The news was edited or deleted by another user."); } } return(View(newsItem)); }
private static void CatchConcurrencyConflict(NewsDBContext context, New loadedEntity) { Console.WriteLine("Text from DB: {0}", loadedEntity.Content); Console.WriteLine("Enter the corrected text:"); string newEntity = Console.ReadLine(); loadedEntity.Content = newEntity; try { context.SaveChanges(); Console.WriteLine("Changes successfully saved in the DB."); } catch (DbUpdateConcurrencyException) { Console.WriteLine("Conflict!"); context.Entry(loadedEntity).Reload(); CatchConcurrencyConflict(context, loadedEntity); } }
public async Task <IActionResult> PutNews([FromRoute] int id, [FromBody] News news) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != news.NewsId) { return(BadRequest()); } //I need to get the date from the context //Datetime needs to be fixed. DateTime today = new DateTime(); today = DateTime.Now; news.UpdatedDate = today; _context.Entry(news).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NewsExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }