Guest User

Untitled

a guest
Dec 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. @model Models.MenuItemsViewModel
  2.  
  3. @{
  4. ViewBag.Title = "Menus";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7. <div class="jumbotron">
  8. <h1 class="display-4">@ViewBag.Title</h1>
  9. <p>here you can set values</p>
  10. </div>
  11.  
  12. @using (Html.BeginForm("Save", "MenuItems", FormMethod.Post))
  13. {
  14. <h2>Menus</h2>
  15. <table class="table">
  16. <thead>
  17. <tr>
  18. <th scope="col">Name</th>
  19. <th scope="col">Status</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td>
  25. @Html.LabelFor(m => m.Schedule)
  26. </td>
  27. <td> @Html.CheckBoxFor(m => m.Schedule, new { @checked = Model.Schedule }) </td>
  28. </tr>
  29. <tr>
  30. <td>
  31. @Html.LabelFor(m => m.FAQ)
  32. </td>
  33. <td> @Html.CheckBoxFor(m => m.FAQ, new { @checked = Model.FAQ }) </td>
  34. </tr>
  35. </tbody>
  36. </table>
  37.  
  38. <input type="submit" value="Save these values" class="btn btn-primary btn-block" />
  39. }
  40.  
  41.  
  42. using System.Linq;
  43. using System.Threading.Tasks;
  44. using System.Web.Mvc;
  45. using Models;
  46.  
  47. namespace Controllers
  48. {
  49. [RoutePrefix("menuItems")]
  50. [Authorize(Roles = LoginController)]
  51. public class MenuItemsController : Controller
  52. {
  53. private readonly AppContext context = new AppContext();
  54.  
  55. [Route("")]
  56. public async Task<ActionResult> Index()
  57. {
  58. var menuItems = context.MenuItems.SingleOrDefault();
  59. return View(new MenuItemsViewModel(menuItems));
  60. }
  61.  
  62. [HttpPost] //I want to call this method when I press save!
  63. public ActionResult Save(MenuItemsViewModel model)
  64. {
  65. var menuItems = context.MenuItems.SingleOrDefault();
  66.  
  67. if (menuItems == null)
  68. {
  69. ViewBag.Message = "Something went wrong";
  70. }
  71.  
  72. model.UpdateFromViewModel(menuItems);
  73.  
  74. ViewBag.Message = "Informationen sparades";
  75. context.SaveChanges();
  76. return RedirectToAction("Index");}
  77. }
  78. }
Add Comment
Please, Sign In to add comment