public ActionResult Edit(int?id, FormCollection editedArticle) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (ModelState.IsValid) { using (var database = new BlogDbContext()) { var article = database.Articles .Where(a => a.Id == id) .First(); if (!IsUserAuthorizedToEdit(article)) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } article.Title = editedArticle["Title"]; article.Content = editedArticle["Content"]; database.Entry(article).State = EntityState.Modified; database.SaveChanges(); return(RedirectToAction("Index")); } } return(View(editedArticle)); }
public ActionResult Edit(string id, EditUserViewModel viewModel) { if (ModelState.IsValid) { using (var db = new BlogDbContext()) { var user = db.Users.FirstOrDefault(u => u.Id.Equals(id)); if (user == null) { // } user.Email = viewModel.User.Email; user.FullName = viewModel.User.FullName; user.UserName = viewModel.User.Email; if (!string.IsNullOrEmpty(viewModel.Password)) { var passwordHasher = new PasswordHasher(); var newPasswordHash = passwordHasher.HashPassword(viewModel.Password); user.PasswordHash = newPasswordHash; } SetUserRoles(user, db, viewModel); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } return(RedirectToAction("List")); } return(View(viewModel)); }
public ActionResult Edit(ArticleViewModel model) { //check if modelstate is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { //Get article from database var article = database.Articles .FirstOrDefault(a => a.Id == model.Id); //Set article properties article.Title = model.Title; article.Content = model.Content; //Save article state in database database.Entry(article).State = EntityState.Modified; database.SaveChanges(); //Redirect to the index page return(RedirectToAction("Index")); } } //if modelstate is invalid return the same view return(View(model)); }
public ActionResult Edit(string id, EditUserViewModel viewModel) { // Check if model is Valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { // Get user from database var user = database.Users .FirstOrDefault(u => u.Id == id); // Check if user exist if (user == null) { return(HttpNotFound()); } // If password field is not empty, change password if (!string.IsNullOrEmpty(viewModel.Password)) { var hasher = new PasswordHasher(); var passwordHash = hasher.HashPassword(viewModel.Password); user.PasswordHash = passwordHash; } // Set user properties user.Email = viewModel.User.Email; user.FullName = viewModel.User.FullName; user.UserName = viewModel.User.Email; this.SetUserRoles(viewModel, user, database); // Save changes database.Entry(user).State = EntityState.Modified; database.SaveChanges(); return(RedirectToAction("List")); } } return(View(viewModel)); }
public async Task <IActionResult> PutZipCode(int id, ZipCode zipCode) { if (id != zipCode.zipcodeId) { return(BadRequest()); } _context.Entry(zipCode).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ZipCodeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult EditConfirmed(ArticleViewModel model) { // Check if model is valid if (ModelState.IsValid) { using (var db = new BlogDbContext()) { // Get article from database var article = db.Articles .Where(a => a.Id == model.Id) .First(); // Set article properties article.Title = model.Title; article.Content = model.Content; // Save article state in database db.Entry(article).State = EntityState.Modified; db.SaveChanges(); // Redirect to the index Page return(RedirectToAction("Index")); } } // If model state is invalid, return the same view return(View(model)); }
public async Task <blg> UpdateBlog(int id, blg body) { _context.Entry(body).State = EntityState.Modified; await _context.SaveChangesAsync(); return(body); }
public async Task <IActionResult> PutAuthor([FromRoute] int id, [FromBody] Author author) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != author.AuthorID) { return(BadRequest()); } _context.Entry(author).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AuthorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } using (var db = new BlogDbContext()) { var article = db.Articles .Include(a => a.Author) .Include(t => t.Tags) .Include(c => c.Comments) .Include(i => i.Images) .Where(a => a.Id == id) .FirstOrDefault(); var user = db.Users.FirstOrDefault(u => u.UserName.Equals(this.User.Identity.Name)); if (user == null || article.AuthorId != user.Id) { article.VisitCounter += 1; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); } return(View(article)); } }
public ActionResult Edit(Article article) { if (ModelState.IsValid) { using (BlogDbContext db = new BlogDbContext()) { db.Articles.Attach(article); db.Entry(article).Property(a => a.Title).IsModified = true; db.Entry(article).Property(a => a.Content).IsModified = true; article.DateModified = DateTime.Now; db.SaveChanges(); return(RedirectToAction("List")); } } return(View(article)); }
public ActionResult Edit(ArticleViewModel model) { //Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { //Get the article from the database var article = database.Articles .FirstOrDefault(a => a.Id == model.Id); //Set the article properties article.Title = model.Title; article.Content = model.Content; article.CategoryId = model.CategoryId; this.SetArticleTags(article, model, database); //Save the article state in the database database.Entry(article).State = EntityState.Modified; database.SaveChanges(); //Redirect to the index page return(RedirectToAction("Details\\" + model.Id)); } } return(View(model)); }
public ActionResult Edit(EventViewModel model) { // Check if model is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { // Get events from database var events = database.Events.FirstOrDefault(a => a.Id == model.Id); // Set event properties events.Id = model.Id; events.Title = model.Title; events.AdditionalContent = model.AdditionalContent; events.Place = model.Place; events.Time = model.Time; events.Date = model.Date; // Save event state in database database.Entry(events).State = EntityState.Modified; database.SaveChanges(); // Redirect to page return(RedirectToAction("List", "Event")); } } // If model state is invalid, return the same view return(View(model)); }
public void UpdateComment(Comment comment) { if (comment != null) { _blogDbContext.Entry(comment).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } }
public void UpdateTag(Tag tag) { if (tag != null) { _blogDbContext.Entry(tag).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } }
public ActionResult Edit(FoodViewModel model) { // Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { // Get salat from database var salat = database.Foods .FirstOrDefault(a => a.Id == model.Id); // Set salat properties salat.Title = model.Title; salat.Content = model.Content; // salat.CategoryId = model.CategoryId; // this.SetsalatTags(salat, model, database); // Save salat state in database database.Entry(salat).State = EntityState.Modified; database.SaveChanges(); // Redirect to the index page return(RedirectToAction("List")); } } // If model state is invalid, return the same view return(View(model)); }
public ActionResult Edit(ArticleViewModel model) { // Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { // Get Article from database var article = database.Articles .FirstOrDefault(a => a.Id == model.Id); if (!IsUserAuthorizedToEdit(article)) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } // Set article properties article.Title = model.Title; article.Content = model.Content; article.CategoryId = model.CategoryId; this.SetArticleTags(article, model, database); // Save article state in database database.Entry(article).State = EntityState.Modified; database.SaveChanges(); // Redirect to the index page return(RedirectToAction("Index")); } } // If model state is invalid, retyrn the same view return(View(model)); }
public async Task <IActionResult> Putseats(int id, seats seats) { if (id != seats.seatsId) { return(BadRequest()); } _context.Entry(seats).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!seatsExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private IHttpActionResult PutBlogCategory(int id, BlogCategory BlogCategory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != BlogCategory.Id) { return(BadRequest()); } db.Entry(BlogCategory).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!BlogCategoryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Edit(string id, EditUserViewModel viewModel) { if (!ModelState.IsValid) { return(View(viewModel)); } using (var database = new BlogDbContext()) { if (!string.IsNullOrEmpty(viewModel.Password)) { ChangeUserPassword(id, viewModel); } var user = database.Users.FirstOrDefault(u => u.Id == id); if (user == null) { return(HttpNotFound()); } this.SetUserRoles(viewModel, user, database); database.Entry(user).State = EntityState.Modified; database.SaveChanges(); return(RedirectToAction("List")); } }
public IHttpActionResult PutComment(Comment comment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Entry(comment).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CommentExists(comment.ID)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutArticle(Guid id, Article article) { if (id != article.Id) { return(BadRequest()); } _context.Entry(article).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void UpdatePost(Post post) { if (post != null) { _blogDbContext.Entry(post).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } }
public ActionResult Edit(ArticleViewModel model) { //Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { //Get article from database var article = database.Articles.Include(a => a.Author).ToList().Find(a => a.Id == model.Id); //Check if user is author or admin if (!IsAuthorizedToEdit(article)) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } //Set article properties article.Title = model.Title; article.Content = model.Content; article.DateAdded = DateTime.Now; //Save article in database database.Entry(article).State = EntityState.Modified; database.SaveChanges(); //Redirect to index page return(RedirectToAction("Index")); } } return(View(model)); }
public ActionResult Edit(ArticleViewModel model) { if (ModelState.IsValid) { using (var db = new BlogDbContext()) { var article = db.Articles.FirstOrDefault(a => a.Id == model.Id); if (article == null) { return(HttpNotFound()); } if (!isAuthorized(article)) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden)); } article.Title = model.Title; article.Content = model.Content; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } return(View(model)); }
public void UpdateCategory(Category category) { if (category != null) { _blogDbContext.Entry(category).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } }
public ActionResult Edit(ArticleViewModel model) { // Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { // Get article from database var article = database.Articles .FirstOrDefault(a => a.Id == model.Id); // Set article properties article.Title = model.Title; article.Content = model.Content; article.CategoryId = model.CategoryId; this.SetArticleTags(article, model, database); // Save article state in database database.Entry(article).State = EntityState.Modified; database.SaveChanges(); //will show message when article is edited this.AddNotification("Article edited.", NotificationType.INFO); // Redirect to the index page return(RedirectToAction("Index")); } } // If model state is invalid, return the same view return(View(model)); }
public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem) { if (id != todoItem.Id) { return BadRequest(); } _context.Entry(todoItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoItemExists(id)) { return NotFound(); } else { throw; } } return NoContent(); }
public ActionResult Edit(CommentViewModels model) { //Check if model state is valid if (ModelState.IsValid) { using (var database = new BlogDbContext()) { //Get comment from database var comment = database.Comments .FirstOrDefault(a => a.Id == model.Id); //Set comment properties comment.Id = model.Id; comment.CommentContent = model.CommentContent; //Save comment state in database database.Entry(comment).State = EntityState.Modified; database.SaveChanges(); //Redirect to the page return(RedirectToAction("Details", "Article", new { id = comment.ArticleId })); } } //if model state is invalid, return the same view return(View(model)); }
public ActionResult Edit(string id, EditUserViewModel viewModel) { if (ModelState.IsValid) { using (var database = new BlogDbContext()) { var user = database.Users.FirstOrDefault(u => u.Id == id); if (user == null) { return(HttpNotFound()); } if (!string.IsNullOrEmpty(viewModel.Password)) { var hasher = new PasswordHasher(); var passwordHash = hasher.HashPassword(viewModel.Password); user.PasswordHash = passwordHash; } user.Email = viewModel.User.Email; user.FullName = viewModel.User.FullName; user.UserName = viewModel.User.Email; this.SetUserRoles(user, database, viewModel); database.Entry(user).State = EntityState.Modified; database.SaveChanges(); return(RedirectToAction("List")); } } return(View(viewModel)); }
public IHttpActionResult PutUser(string id, User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != user.Uid) { return(BadRequest()); } db.Entry(user).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }