Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. public JsonResult LoadOccupantsDetailedInformation()
  2. {
  3. //Load the personsFormModel with data
  4. return new JsonpResult(personsFormModel, JsonRequestBehavior.AllowGet);
  5. }
  6.  
  7. [HttpPost]
  8. public ActionResult SaveOccupantsDetailedInformation(
  9. PersonsFormModel personsFormModel)
  10. {
  11. //This line is always returning true even if I pass 2 persons with the same ssn
  12. if (ModelState.IsValid == false)
  13. {
  14. var errorList = ModelState.ToDictionary(
  15. kvp => kvp.Key,
  16. kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
  17. );
  18. return Json(new { Errors = errorList });
  19. }
  20. //Save the data in personsFormModel to database
  21. return Json(new JsonResultViewModel { Success = true });
  22. }
  23.  
  24.  
  25. public partial class PersonsFormModel : List<PersonFormModel>, IValidatableObject
  26. {
  27. public IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> Validate(
  28. ValidationContext validationContext)
  29. {
  30. var validationResults
  31. = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
  32. if (this.SSNs.Count() > this.SSNs.Distinct().Count())
  33. {
  34. validationResults.Add(new System.ComponentModel.DataAnnotations.ValidationResult(
  35. "All the persons in the household should have a unique SSN\ITIN number",
  36. new[] { "SSN" }));
  37. }
  38. return validationResults;
  39. }
  40. private IEnumerable<string> SSNs
  41. {
  42. get
  43. {
  44. return this.Select(element => element.SSN);
  45. }
  46. }
  47. }
  48. public class PrequalifyOccupantListPersonDetailedFormModel
  49. {
  50. [Required(ErrorMessage = "SSN is required")]
  51. public string SSN { get; set; }
  52. }
  53.  
  54. [Test]
  55. public void Should_validate_person_form_model()
  56. {
  57. var input = new PersonsFormModel();
  58. input.Add(new PersonFormModel { SSN = "33" });
  59. input.Add(new PersonFormModel { SSN = "33" });
  60. _controller.ViewData.ModelState.Clear();
  61.  
  62. var results = new List<ValidationResult>();
  63.  
  64. bool isValid = Validator.TryValidateObject(input,
  65. new ValidationContext(input, null, null),
  66. results,true);
  67.  
  68. Assert.IsTrue(!isValid, "Cannot have duplicates.");
  69. }
  70.  
  71. ***Controller:***
  72. public JsonResult LoadOccupantsDetailedInformation()
  73. {
  74. //Load the personsFormModel with data
  75. return new JsonpResult(personsFormModel, JsonRequestBehavior.AllowGet);
  76. }
  77.  
  78. [HttpPost]
  79. public ActionResult SaveOccupantsDetailedInformation(
  80. PersonsFormModel personsFormModel)
  81. {
  82. //This line is always returning true even if I pass 2 persons with the same ssn
  83. if (ModelState.IsValid == false)
  84. {
  85. var errorList = ModelState.ToDictionary(
  86. kvp => kvp.Key,
  87. kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
  88. );
  89. return Json(new { Errors = errorList });
  90. }
  91. //Save the data in personsFormModel to database
  92. return Json(new JsonResultViewModel { Success = true });
  93. }
  94.  
  95. ***Model:***
  96. public class PersonsFormModel : IValidatableObject
  97. {
  98. public PersonsFormModel()
  99. {
  100. this.Instance = new List<PersonFormModel>();
  101. }
  102.  
  103. public List<PrequalifyOccupantListPersonFormModel> Instance { get; set; }
  104.  
  105. public void Add(PersonFormModel personFormModel)
  106. {
  107. Instance.Add(personFormModel);
  108. }
  109.  
  110. public IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> Validate(
  111. ValidationContext validationContext)
  112. {
  113. var validationResults
  114. = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
  115. if (this.SSNs.Count() > this.SSNs.Distinct().Count())
  116. {
  117. validationResults.Add(new System.ComponentModel.DataAnnotations.ValidationResult(
  118. "All the persons in the household should have a unique SSN\ITIN number",
  119. new[] { "SSN" }));
  120. }
  121. return validationResults;
  122. }
  123. private IEnumerable<string> SSNs
  124. {
  125. get
  126. {
  127. return Instance.Select(element => element.SSN);
  128. }
  129. }
  130. }
  131.  
  132. public class PersonFormModel
  133. {
  134. [Required(ErrorMessage = "SSN is required")]
  135. public string SSN { get; set; }
  136. }
  137.  
  138. View:
  139.  
  140. // Note, I am passing the "Instance" here.
  141.  
  142. var postData = JSON.stringify({ Instance: list });
  143.  
  144. RealPage.DataManager.POST({
  145. url: "/Approval/SaveOccupantsDetailedInformation"
  146. , data: postData
  147. , success: function (data) {
  148. if (data && data.Success) {
  149.  
  150. }
  151. }
  152. });
Add Comment
Please, Sign In to add comment