Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpGet]
- public ActionResult Create(string uniqueUri)
- {
- // get some stuff based on uniqueuri, set in ViewData.
- return View();
- }
- [HttpPost]
- public ActionResult Create(Review review)
- {
- // validate review
- if (validatedOk)
- {
- return RedirectToAction("Details", new { postId = review.PostId});
- }
- else
- {
- ModelState.AddModelError("ReviewErrors", "some error occured");
- return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
- }
- }
- [HttpGet]
- public ActionResult Create(string uniqueUri)
- {
- //Restore
- Review review = TempData["Review"] as Review;
- // get some stuff based on uniqueuri, set in ViewData.
- return View(review);
- }
- [HttpPost]
- public ActionResult Create(Review review)
- {
- //Save you object
- TempData["Review"] = review;
- // validate review
- if (validatedOk)
- {
- return RedirectToAction("Details", new { postId = review.PostId});
- }
- else
- {
- ModelState.AddModelError("ReviewErrors", "some error occured");
- return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
- }
- }
- Review review = TempData["Review"] as Review;
- TempData["Review"] = review;
- public class SetTempDataModelStateAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- base.OnActionExecuted(filterContext);
- filterContext.Controller.TempData["ModelState"] =
- filterContext.Controller.ViewData.ModelState;
- }
- }
- public class RestoreModelStateFromTempDataAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- base.OnActionExecuting(filterContext);
- if (filterContext.Controller.TempData.ContainsKey("ModelState"))
- {
- filterContext.Controller.ViewData.ModelState.Merge(
- (ModelStateDictionary)filterContext.Controller.TempData["ModelState"]);
- }
- }
- }
- [HttpGet]
- [RestoreModelStateFromTempData]
- public ActionResult Create(string uniqueUri)
- {
- // get some stuff based on uniqueuri, set in ViewData.
- return View();
- }
- [HttpPost]
- [SetTempDataModelState]
- public ActionResult Create(Review review)
- {
- // validate review
- if (validatedOk)
- {
- return RedirectToAction("Details", new { postId = review.PostId});
- }
- else
- {
- ModelState.AddModelError("ReviewErrors", "some error occured");
- return RedirectToAction("Create", new { uniqueUri = Request.RequestContext.RouteData.Values["uniqueUri"]});
- }
- }
- public class GetStuffBasedOnUniqueUriAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- var filter = new GetStuffBasedOnUniqueUriFilter();
- filter.OnActionExecuting(filterContext);
- }
- }
- public class GetStuffBasedOnUniqueUriFilter : IActionFilter
- {
- #region IActionFilter Members
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- }
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- filterContext.Controller.ViewData["somekey"] = filterContext.RouteData.Values["uniqueUri"];
- }
- #endregion
- }
- [HttpGet, GetStuffBasedOnUniqueUri]
- public ActionResult Create()
- {
- return View();
- }
- [HttpPost, GetStuffBasedOnUniqueUri]
- public ActionResult Create(Review review)
- {
- // validate review
- if (validatedOk)
- {
- return RedirectToAction("Details", new { postId = review.PostId });
- }
- ModelState.AddModelError("ReviewErrors", "some error occured");
- return View(review);
- }
- @Html.ValidationSummary()
- @using (Html.BeginForm("Summary", "MyController", FormMethod.Post))
- {
- @Html.Hidden("TelNo") @* // Javascript to update this *@
- [HttpPost]
- public ActionResult Summary(EditedItemsVM vm)
- // Telephone number wasn't in the right format
- List<string> listOfErrors = new List<string>();
- listOfErrors.Add("Telephone Number was not in the correct format. Value supplied was: " + vm.TelNo);
- TempData["SummaryEditedErrors"] = listOfErrors;
- return RedirectToAction("Summary");
- [HttpGet]
- [OutputCache(Duration = 0)]
- public ActionResult Summary()
- {
- // setup, including retrieval of the viewmodel from TempData...
- // And finally if we are coming back to this after a failed attempt to edit some of the fields on the page,
- // load the errors stored from TempData.
- List<string> editErrors = new List<string>();
- object errData = TempData["SummaryEditedErrors"];
- if (errData != null)
- {
- editErrors = (List<string>)errData;
- foreach(string err in editErrors)
- {
- // ValidationSummary() will see these
- ModelState.AddModelError("", err);
- }
- }
- public class RegisterViewModel
- {
- public string FirstName { get; set; }
- public IList<Gender> Genders { get; set; }
- //Some other properties here ....
- //...
- //...
- ViewModelType PopulateDefaultViewData()
- {
- this.FirstName = "No body";
- this.Genders = new List<Gender>()
- {
- Gender.Male,
- Gender.Female
- };
- //Maybe other assinments here for other properties...
- }
- }
- [HttpGet]
- public async Task<IActionResult> Register()
- {
- var vm = new RegisterViewModel().PopulateDefaultViewValues();
- return View(vm);
- }
- [HttpPost]
- public async Task<IActionResult> Register(RegisterViewModel vm)
- {
- if (!ModelState.IsValid)
- {
- return View(vm.PopulateDefaultViewValues());
- }
- var user = await userService.RegisterAsync(
- email: vm.Email,
- password: vm.Password,
- firstName: vm.FirstName,
- lastName: vm.LastName,
- gender: vm.Gender,
- birthdate: vm.Birthdate);
- return Json("Registered successfully!");
- }
Add Comment
Please, Sign In to add comment