Guest User

Untitled

a guest
Sep 12th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. Post a list property along with other model properties in JQuery Ajax to MVC controller?
  2. public class EducationalBackground
  3. {
  4. public int EducationalBackgroundID { get; set; }
  5.  
  6. public string UniversityOrCollege { get; set; }
  7.  
  8. public string AreaOfStudy { get; set; }
  9.  
  10. public string Degree { get; set; }
  11.  
  12. public string YearReceived { get; set; }
  13. }
  14.  
  15. public class InstructorApplicationViewModel
  16. {
  17. public InstructorApplicationViewModel()
  18. {
  19. EducationalBackgrounds = new List<EducationalBackGround>
  20. {
  21. new EducationalBackGround {
  22. AreaOfStudy = string.Empty,
  23. Degree = string.Empty,
  24. UniversityOrCollege = string.Empty,
  25. YearReceived = string.Empty
  26. }
  27. };
  28. }
  29.  
  30. public IList<EducationalBackGround> EducationalBackgrounds { get; set; }
  31.  
  32. public int CurrentUserId { get; set; }
  33.  
  34. [Required]
  35. public string Experience { get; set; }
  36.  
  37. [Required]
  38. public bool WillingToTravel { get; set; }
  39. }
  40.  
  41. @model PaulSchool.ViewModels.InstructorApplicationViewModel
  42. @{
  43. ViewBag.Title = "ApplyToBecomeInstructor";
  44. }
  45. <h2>
  46. ApplyToBecomeInstructor</h2>
  47. <script src="@Url.Content("~/Scripts/EducationalBackgroundListEditor.js")" type="text/javascript"> </script>
  48. @using (Html.BeginForm())
  49. {
  50. <fieldset>
  51. <legend>ApplyToBecomeInstructor</legend>
  52. <div class="editor-label">
  53. <p>
  54. Information from your user profile will be considered when applying to become an
  55. Instructor</p>
  56. </div>
  57. @Html.HiddenFor(model => model.CurrentUserId)
  58. <div class="editor-label">
  59. @Html.LabelFor(model => model.EducationalBackgrounds)
  60. </div>
  61. <div id="editorRows">
  62. @Html.EditorFor(model => model.EducationalBackgrounds)
  63. </div>
  64. @Html.ActionLink("Add additional educational background.", "EducationalBackground", null, new { id = "addItem" })
  65. <div class="editor-label">
  66. @Html.LabelFor(model => model.Experience)
  67. </div>
  68. <div class="editor-field">
  69. @Html.EditorFor(model => model.Experience)
  70. </div>
  71. <div class="editor-label">
  72. Are you willing to travel?
  73. </div>
  74. <div class="editor-field">
  75. @Html.EditorFor(model => model.WillingToTravel)
  76. </div>
  77. <p>
  78. <input type="submit" value="Create" id="btnSubmit" />
  79. </p>
  80. <div class="newRow" style="display: none;">
  81. <div class="editorRow">
  82. <p>
  83. <label>
  84. UniversityOrCollege</label>
  85. <input class="university" type="text" value="" />
  86. </p>
  87. <p>
  88. <label>
  89. AreaOfStudy</label>
  90. <input class="area" type="text" value="" />
  91. </p>
  92. <p>
  93. <label>
  94. Degree</label>
  95. <input class="degree" type="text" value="" />
  96. </p>
  97. <p>
  98. <label>
  99. YearReceived</label>
  100. <input class="year" type="text" value="" />
  101. </p>
  102. <div>
  103. <a href="javascript:void(0);" class="deleteRow">Delete</a>
  104. </div>
  105. </div>
  106. </div>
  107. </fieldset>
  108. }
  109. <div>
  110. @Html.ActionLink("Back", "Index")
  111. </div>
  112.  
  113. @model PaulSchool.ViewModels.EducationalBackGround
  114. <div class="editorRow">
  115. <p>
  116. @Html.LabelFor(model => model.UniversityOrCollege)
  117. @Html.TextBoxFor(model => model.UniversityOrCollege, new { @class="university" })
  118. </p>
  119. <p>
  120. @Html.LabelFor(model => model.AreaOfStudy)
  121. @Html.TextBoxFor(model => model.AreaOfStudy, new { @class="area" })
  122. </p>
  123. <p>
  124. @Html.LabelFor(model => model.Degree)
  125. @Html.TextBoxFor(model => model.Degree, new { @class = "degree" })
  126. </p>
  127. <p>
  128. @Html.LabelFor(model => model.YearReceived)
  129. @Html.TextBoxFor(model => model.YearReceived, new { @class = "year" })
  130. </p>
  131. <div>
  132. <a href="javascript:void(0);" class="deleteRow">Delete</a>
  133. </div>
  134. </div>
  135.  
  136. <script type="text/javascript">
  137. $('#btnSubmit').click(function () {
  138. instructorUrl = '@Url.Action("ApplyToBecomeInstructor", "InstructorApplication")';
  139. var user = [];
  140. var educationList = [];
  141. var currentUser = '@Model.CurrentUserId';
  142. var experience = $('#Experience').val();
  143. var isWilling = $('#WillingToTravel').is(":checked");
  144. $('#editorRows .editorRow').each(function () {
  145. var education = {
  146. UniversityOrCollege: $(this).find('.university').val(),
  147. AreaOfStudy: $(this).find('.area').val(),
  148. Degree: $(this).find('.degree').val(),
  149. YearReceived: $(this).find('.year').val()
  150. }
  151. educationList.push(education);
  152. });
  153. var applicationFromView = {
  154. EducationalBackgrounds: educationList,
  155. CurrentUserId: currentUser,
  156. Experience: experience,
  157. WillingToTravel: isWilling
  158. }
  159. $.ajax({
  160. type: 'POST',
  161. url: instructorUrl,
  162. dataType: "json",
  163. data: applicationFromView,
  164. success: function (data) {
  165. },
  166. error: function (a, b, c) {
  167. alert('A problem ocurred');
  168. }
  169. });
  170. });
  171.  
  172. </script>
  173.  
  174. [HttpPost]
  175. public ActionResult ApplyToBecomeInstructor(InstructorApplicationViewModel applicationFromView)
  176. {
  177. Student thisStudent = this.db.Students.FirstOrDefault(o => o.StudentID == applicationFromView.CurrentUserId);
  178. var instructorApplication = new InstructorApplication
  179. {
  180. BasicInfoGatheredFromProfile = thisStudent,
  181. EducationalBackground = applicationFromView.EducationalBackgrounds as ICollection<EducationalBackground>,
  182. Experience = applicationFromView.Experience,
  183. WillingToTravel = applicationFromView.WillingToTravel
  184. };
  185. this.db.InstructorApplication.Add(instructorApplication);
  186. this.db.SaveChanges();
  187. return this.Redirect("Index");
  188. }
  189.  
  190. $.ajax({
  191. type: 'POST',
  192. url: instructorUrl,
  193. dataType: "json",
  194. data: JSON.stringify(applicationFromView), //<-- JSON.stringify
  195. contentType: 'application/json; charset=utf-8', //<-- contentType
  196. success: function (data) {
  197. },
  198. error: function (a, b, c) {
  199. alert('A problem ocurred');
  200. }
  201. });
Add Comment
Please, Sign In to add comment