Guest User

Untitled

a guest
Dec 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. [HttpGet]
  2. public ActionResult Create(string uniqueUri)
  3. {
  4. // get some stuff based on uniqueuri, set in ViewData.
  5. return View();
  6. }
  7.  
  8. [HttpPost]
  9. public ActionResult Create(Review review)
  10. {
  11. // validate review
  12. if (validatedOk)
  13. {
  14. return RedirectToAction("Details", new { postId = review.PostId});
  15. }
  16. else
  17. {
  18. ModelState.AddModelError("ReviewErrors", "some error occured");
  19. return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
  20. }
  21. }
  22.  
  23. [HttpGet]
  24. public ActionResult Create(string uniqueUri)
  25. {
  26. //Restore
  27. Review review = TempData["Review"] as Review;
  28.  
  29. // get some stuff based on uniqueuri, set in ViewData.
  30. return View(review);
  31. }
  32. [HttpPost]
  33. public ActionResult Create(Review review)
  34. {
  35. //Save you object
  36. TempData["Review"] = review;
  37.  
  38. // validate review
  39. if (validatedOk)
  40. {
  41. return RedirectToAction("Details", new { postId = review.PostId});
  42. }
  43. else
  44. {
  45. ModelState.AddModelError("ReviewErrors", "some error occured");
  46. return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
  47. }
  48. }
  49.  
  50. Review review = TempData["Review"] as Review;
  51. TempData["Review"] = review;
  52.  
  53. public class SetTempDataModelStateAttribute : ActionFilterAttribute
  54. {
  55. public override void OnActionExecuted(ActionExecutedContext filterContext)
  56. {
  57. base.OnActionExecuted(filterContext);
  58. filterContext.Controller.TempData["ModelState"] =
  59. filterContext.Controller.ViewData.ModelState;
  60. }
  61. }
  62.  
  63. public class RestoreModelStateFromTempDataAttribute : ActionFilterAttribute
  64. {
  65. public override void OnActionExecuting(ActionExecutingContext filterContext)
  66. {
  67. base.OnActionExecuting(filterContext);
  68. if (filterContext.Controller.TempData.ContainsKey("ModelState"))
  69. {
  70. filterContext.Controller.ViewData.ModelState.Merge(
  71. (ModelStateDictionary)filterContext.Controller.TempData["ModelState"]);
  72. }
  73. }
  74. }
  75.  
  76. [HttpGet]
  77. [RestoreModelStateFromTempData]
  78. public ActionResult Create(string uniqueUri)
  79. {
  80. // get some stuff based on uniqueuri, set in ViewData.
  81. return View();
  82. }
  83.  
  84. [HttpPost]
  85. [SetTempDataModelState]
  86. public ActionResult Create(Review review)
  87. {
  88. // validate review
  89. if (validatedOk)
  90. {
  91. return RedirectToAction("Details", new { postId = review.PostId});
  92. }
  93. else
  94. {
  95. ModelState.AddModelError("ReviewErrors", "some error occured");
  96. return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
  97. }
  98. }
  99.  
  100. public class GetStuffBasedOnUniqueUriAttribute : ActionFilterAttribute
  101. {
  102. public override void OnActionExecuting(ActionExecutingContext filterContext)
  103. {
  104. var filter = new GetStuffBasedOnUniqueUriFilter();
  105.  
  106. filter.OnActionExecuting(filterContext);
  107. }
  108. }
  109.  
  110.  
  111. public class GetStuffBasedOnUniqueUriFilter : IActionFilter
  112. {
  113. #region IActionFilter Members
  114.  
  115. public void OnActionExecuted(ActionExecutedContext filterContext)
  116. {
  117.  
  118. }
  119.  
  120. public void OnActionExecuting(ActionExecutingContext filterContext)
  121. {
  122. filterContext.Controller.ViewData["somekey"] = filterContext.RouteData.Values["uniqueUri"];
  123. }
  124.  
  125. #endregion
  126. }
  127.  
  128. [HttpGet, GetStuffBasedOnUniqueUri]
  129. public ActionResult Create()
  130. {
  131. return View();
  132. }
  133.  
  134. [HttpPost, GetStuffBasedOnUniqueUri]
  135. public ActionResult Create(Review review)
  136. {
  137. // validate review
  138. if (validatedOk)
  139. {
  140. return RedirectToAction("Details", new { postId = review.PostId });
  141. }
  142.  
  143. ModelState.AddModelError("ReviewErrors", "some error occured");
  144. return View(review);
  145. }
  146.  
  147. @Html.ValidationSummary()
  148.  
  149. @using (Html.BeginForm("Summary", "MyController", FormMethod.Post))
  150. {
  151. @Html.Hidden("TelNo") @* // Javascript to update this *@
  152.  
  153. [HttpPost]
  154. public ActionResult Summary(EditedItemsVM vm)
  155.  
  156. // Telephone number wasn't in the right format
  157. List<string> listOfErrors = new List<string>();
  158. listOfErrors.Add("Telephone Number was not in the correct format. Value supplied was: " + vm.TelNo);
  159. TempData["SummaryEditedErrors"] = listOfErrors;
  160. return RedirectToAction("Summary");
  161.  
  162. [HttpGet]
  163. [OutputCache(Duration = 0)]
  164. public ActionResult Summary()
  165. {
  166. // setup, including retrieval of the viewmodel from TempData...
  167.  
  168.  
  169. // And finally if we are coming back to this after a failed attempt to edit some of the fields on the page,
  170. // load the errors stored from TempData.
  171. List<string> editErrors = new List<string>();
  172. object errData = TempData["SummaryEditedErrors"];
  173. if (errData != null)
  174. {
  175. editErrors = (List<string>)errData;
  176. foreach(string err in editErrors)
  177. {
  178. // ValidationSummary() will see these
  179. ModelState.AddModelError("", err);
  180. }
  181. }
  182.  
  183. public class RegisterViewModel
  184. {
  185. public string FirstName { get; set; }
  186. public IList<Gender> Genders { get; set; }
  187. //Some other properties here ....
  188. //...
  189. //...
  190.  
  191. ViewModelType PopulateDefaultViewData()
  192. {
  193. this.FirstName = "No body";
  194. this.Genders = new List<Gender>()
  195. {
  196. Gender.Male,
  197. Gender.Female
  198. };
  199.  
  200. //Maybe other assinments here for other properties...
  201. }
  202. }
  203.  
  204. [HttpGet]
  205. public async Task<IActionResult> Register()
  206. {
  207. var vm = new RegisterViewModel().PopulateDefaultViewValues();
  208. return View(vm);
  209. }
  210.  
  211. [HttpPost]
  212. public async Task<IActionResult> Register(RegisterViewModel vm)
  213. {
  214. if (!ModelState.IsValid)
  215. {
  216. return View(vm.PopulateDefaultViewValues());
  217. }
  218.  
  219. var user = await userService.RegisterAsync(
  220. email: vm.Email,
  221. password: vm.Password,
  222. firstName: vm.FirstName,
  223. lastName: vm.LastName,
  224. gender: vm.Gender,
  225. birthdate: vm.Birthdate);
  226.  
  227. return Json("Registered successfully!");
  228. }
Add Comment
Please, Sign In to add comment