Advertisement
Guest User

ValidateAntiForgeryToken

a guest
Dec 30th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. public IActionResult Edit(int id) {
  2.  
  3. var model = this.categoryService.GetById(id);
  4. return View(model);
  5. }
  6.  
  7. [HttpPost]
  8. [ValidateAntiForgeryToken]
  9. public IActionResult Edit(CategoriesViewModel model) {
  10.  
  11. if (ModelState.IsValid) {
  12. this.categoryService.Update(model);
  13. return RedirectToAction(nameof(Category));
  14. }
  15.  
  16. return this.View(model.Id);
  17. }
  18.  
  19. //View
  20. @using CakeStore.App.Areas.Admin.Models.Categories;
  21. @model CategoriesViewModel
  22. @{
  23. ViewData["Title"] = "Edit";
  24. Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
  25.  
  26. }
  27.  
  28. <h1 class="text-center text-header-page">Edit Category</h1>
  29. <hr class="hr-admin-divider" />
  30. <form class="mx-auto w-50 form-horizontal" method="post" action="Edit">
  31. <div asp-validation-summary="ModelOnly" class="text-danger"></div>
  32. <div class="form-group">
  33. <input asp-for="@Model.Id" type="hidden" name="id" value="@Model.Id"/>
  34. </div>
  35. <div class="form-group">
  36. <label asp-for="@Model.Name"></label>
  37. <input asp-for="@Model.Name" type="text" value="@Model.Name" class="form-control" id="name" name="name">
  38. <span asp-validation-for="@Model.Name" class="text-danger"></span>
  39. </div>
  40. <div class="button-holder d-flex justify-content-center">
  41. <button type="submit" class="btn button-black-white">Edit</button>
  42. </div>
  43. </form>
  44.  
  45. //Model
  46. namespace CakeStore.App.Areas.Admin.Models.Categories {
  47. public class CategoriesViewModel {
  48. public int Id { get; set; }
  49. [Required]
  50. [StringLength(22,MinimumLength =3,ErrorMessage =AdminConstants.NameRange)]
  51. public string Name { get; set; }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement