Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. protected override StudentDTO CreateEntity([FromBody]StudentDTO entityDto)
  2. {
  3. if (ModelState.IsValid)
  4. {
  5. var entity = entityDto.ToEntity();
  6. _context.Students.Add(entity);
  7. _context.SaveChanges();
  8. return new StudentDTO(_context.Students
  9. .Include(p => p.ID_RoomForeignKey)
  10. .Include(s => s.ID_UserNumber)
  11. .FirstOrDefault(p => p.ID_Student == entity.ID_Student));
  12. }
  13. else
  14. {
  15. HttpResponseMessage response = null;
  16. //response = Request.CreateResponse(HttpStatusCode.BadRequest, new ODataError
  17. //{
  18. // ErrorCode = "ValidationError",
  19. // Message = String.Join(";", ModelState.Values.First().Errors.Select(e => e.ErrorMessage).ToArray())
  20.  
  21. //});
  22. throw new HttpResponseException(response);
  23. }
  24. }
  25.  
  26. public class Student
  27. {
  28. [Key]
  29. public int ID_Student { get; set; }
  30.  
  31. [Required]
  32. [StringLength(500)]
  33. public string FIOStudent { get; set; }
  34. public DateTime BirthDate { get; set; }
  35. [Required]
  36. [StringLength(500)]
  37. public string Faculty { get; set; }
  38. [Required]
  39. [StringLength(500)]
  40. public string Group { get; set; }
  41. [Required]
  42. [StringLength(500)]
  43. public string EducationalForm { get; set; }
  44. public int ID_Room { get; set; }
  45. public int id_user { get; set; }
  46. public virtual Room ID_RoomForeignKey { get; set; }
  47. public virtual User ID_UserNumber { get; set; }
  48.  
  49. }
  50.  
  51. public class StudentDTO
  52. {
  53. public StudentDTO() {
  54.  
  55. }
  56. public StudentDTO(Student student)
  57. {
  58. ID_Student = student.ID_Student;
  59. FIOStudent = student.FIOStudent;
  60. BirthDate = student.BirthDate;
  61. Faculty = student.Faculty;
  62. Group = student.Group;
  63. EducationalForm = student.EducationalForm;
  64. ID_Room = student.ID_Room;
  65. TypeRoom = student.ID_RoomForeignKey.TypeRoom;
  66. Floor = student.ID_RoomForeignKey.Floor;
  67. user_id = student.ID_UserNumber.user_id;
  68. Login = student.ID_UserNumber.Login;
  69. Password = student.ID_UserNumber.Password;
  70.  
  71. }
  72. [Key]
  73. public int ID_Student { get; set; }
  74.  
  75. [Required]
  76. [StringLength(500)]
  77. public string FIOStudent { get; set; }
  78. public DateTime BirthDate { get; set; }
  79. [Required]
  80. [StringLength(500)]
  81. public string Faculty { get; set; }
  82. [Required]
  83. [StringLength(500)]
  84. public string Group { get; set; }
  85. [Required]
  86. [StringLength(500)]
  87. public string EducationalForm { get; set; }
  88. public int ID_Room { get; set; }
  89. //public int id_user { get; set; }
  90. //public int ID_Room { get; set; }
  91. public string TypeRoom { get; set; }
  92. public int Floor { get; set; }
  93.  
  94. public int user_id { get; set; }
  95. [Required]
  96. [StringLength(500)]
  97. public string Password { get; set; }
  98. [Required]
  99. [StringLength(500)]
  100. public string Login { get; set; }
  101.  
  102. public Student ToEntity()
  103. {
  104. return new Student
  105. {
  106. ID_Student = ID_Student,
  107. FIOStudent = FIOStudent,
  108. BirthDate = BirthDate,
  109. Faculty = Faculty,
  110. Group = Group,
  111. EducationalForm = EducationalForm,
  112. ID_Room = ID_Room,
  113. id_user = user_id
  114.  
  115. };
  116. }
  117. }
  118.  
  119. <div id="createBlock" float="right" display="inline-block" margin="20px">
  120. <h3>Добавление </h3>
  121. <table>
  122. <tr><td><label>ФИО: </label></td><td><input type="text" id="editFIO" /></td></tr>
  123. <tr><td><label>Дата Рождения: </label></td><td><input type="text" id="editBirthDate" /></td></tr>
  124. <tr><td><label>Факультет: </label></td><td><input type="text" id="editFaculty" /></td></tr>
  125. <tr><td><label>Группа: </label></td><td><input type="text" id="editGroup" /></td></tr>
  126. <tr><td><label>Форма обучения: </label></td><td><input type="text" id="editEduForm" /></td></tr>
  127. <tr><td><label>Номер комнаты: </label></td><td><input type="number" id="editNumberRoom" /></td></tr>
  128. </table>
  129. <button id="addStudent">Сохранить</button>
  130. </div>
  131.  
  132. <script type="text/javascript">
  133. $(document).ready(function () {
  134.  
  135. GetAllStudents();
  136.  
  137. $("#addStudent").click(function (event) {
  138. event.preventDefault();
  139. AddStudent();
  140. });
  141.  
  142. });
  143.  
  144. function AddStudent() {
  145.  
  146. var student = {
  147. ID_Student: $('#editId').val(),
  148. FIOStudent: $('#editFIO').val(),
  149. BirthDate: $('#editBirthDate').val(),
  150. Faculty: $('#editFaculty').val(),
  151. Group: $('#editGroup').val(),
  152. EducationalForm: $('#editEduForm').val(),
  153. ID_Room: $('#editNumberRoom').val()
  154. };
  155.  
  156. $.ajax({
  157. url: '/odata/Student',
  158. type: 'POST',
  159. data: JSON.stringify(student),
  160. contentType: "application/json;charset=utf-8",
  161. success: function (data) {
  162. GetAllStudents();
  163. },
  164. error: function (x, y, z) {
  165. alert(x + 'n' + y + 'n' + z);
  166. }
  167. });
  168. }
  169. function GetAllStudents() {
  170.  
  171. $("#createBlock").css('display', 'block');
  172. $.ajax({
  173. url: '/odata/Student',
  174. type: 'Get',
  175. dataType: 'JSON',
  176. contentType: "application/json;charset=utf-8",
  177. async: false,
  178. success: function (data) {
  179. WriteResponse(data.value);
  180. },
  181. error: function (x, y, z) {
  182. alert(x + 'n' + y + 'n' + z);
  183. }
  184. });
  185. }
  186. </script>
  187.  
  188. ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
  189. modelBuilder.EntitySet<Student>("Student");
  190. modelBuilder.EntitySet<StudentDTO>("StudentDTO");
  191.  
  192. modelBuilder.EntitySet<StudentDTO>("Student");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement