Advertisement
otkalce

Error handling

May 5th, 2023 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | Source Code | 0 0
  1. *** Models/MonthEnum.cs ***
  2. public enum MonthEnum : int
  3. {
  4.     January = 0,
  5.     February,
  6.     March,
  7.     April,
  8.     May,
  9.     June,
  10.     July,
  11.     August,
  12.     September,
  13.     October,
  14.     November,
  15.     December
  16. }
  17.  
  18. *** Models/MonthEnum.cs ***
  19. [HttpPost]
  20. public IActionResult Index(PersonalData personalData)
  21. {
  22.     if (!ModelState.IsValid)
  23.         return View(personalData);
  24.  
  25.     var monthOfBirth = (MonthEnum)personalData.DateOfBirth.Month;
  26.     var shortMonth = monthOfBirth.ToString().Substring(0, 3);
  27.  
  28.     TempData["personName"] = personalData.FirstName + " " + personalData.LastName;
  29.     TempData["personBirthMonth"] = shortMonth;
  30.  
  31.     return RedirectToAction("Success");
  32. }
  33.  
  34. *** Success.cshtml ***
  35. @{
  36.     var personName = TempData["personName"] as string;
  37.     var personBirthMonth = TempData["personBirthMonth"] as string;
  38. }
  39.  
  40. <h1>You have succesfully submitted the data</h1>
  41. <hr />
  42. <div class="text-primary">Name: @personName</div>
  43. <div class="text-primary">Month of birth: @personBirthMonth</div>
  44. <hr />
  45. <a asp-controller="Home" asp-action="Index" class="btn-sm btn-primary">Back</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement