Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override StudentDTO CreateEntity([FromBody]StudentDTO entityDto)
- {
- if (ModelState.IsValid)
- {
- var entity = entityDto.ToEntity();
- _context.Students.Add(entity);
- _context.SaveChanges();
- return new StudentDTO(_context.Students
- .Include(p => p.ID_RoomForeignKey)
- .Include(s => s.ID_UserNumber)
- .FirstOrDefault(p => p.ID_Student == entity.ID_Student));
- }
- else
- {
- HttpResponseMessage response = null;
- //response = Request.CreateResponse(HttpStatusCode.BadRequest, new ODataError
- //{
- // ErrorCode = "ValidationError",
- // Message = String.Join(";", ModelState.Values.First().Errors.Select(e => e.ErrorMessage).ToArray())
- //});
- throw new HttpResponseException(response);
- }
- }
- public class Student
- {
- [Key]
- public int ID_Student { get; set; }
- [Required]
- [StringLength(500)]
- public string FIOStudent { get; set; }
- public DateTime BirthDate { get; set; }
- [Required]
- [StringLength(500)]
- public string Faculty { get; set; }
- [Required]
- [StringLength(500)]
- public string Group { get; set; }
- [Required]
- [StringLength(500)]
- public string EducationalForm { get; set; }
- public int ID_Room { get; set; }
- public int id_user { get; set; }
- public virtual Room ID_RoomForeignKey { get; set; }
- public virtual User ID_UserNumber { get; set; }
- }
- public class StudentDTO
- {
- public StudentDTO() {
- }
- public StudentDTO(Student student)
- {
- ID_Student = student.ID_Student;
- FIOStudent = student.FIOStudent;
- BirthDate = student.BirthDate;
- Faculty = student.Faculty;
- Group = student.Group;
- EducationalForm = student.EducationalForm;
- ID_Room = student.ID_Room;
- TypeRoom = student.ID_RoomForeignKey.TypeRoom;
- Floor = student.ID_RoomForeignKey.Floor;
- user_id = student.ID_UserNumber.user_id;
- Login = student.ID_UserNumber.Login;
- Password = student.ID_UserNumber.Password;
- }
- [Key]
- public int ID_Student { get; set; }
- [Required]
- [StringLength(500)]
- public string FIOStudent { get; set; }
- public DateTime BirthDate { get; set; }
- [Required]
- [StringLength(500)]
- public string Faculty { get; set; }
- [Required]
- [StringLength(500)]
- public string Group { get; set; }
- [Required]
- [StringLength(500)]
- public string EducationalForm { get; set; }
- public int ID_Room { get; set; }
- //public int id_user { get; set; }
- //public int ID_Room { get; set; }
- public string TypeRoom { get; set; }
- public int Floor { get; set; }
- public int user_id { get; set; }
- [Required]
- [StringLength(500)]
- public string Password { get; set; }
- [Required]
- [StringLength(500)]
- public string Login { get; set; }
- public Student ToEntity()
- {
- return new Student
- {
- ID_Student = ID_Student,
- FIOStudent = FIOStudent,
- BirthDate = BirthDate,
- Faculty = Faculty,
- Group = Group,
- EducationalForm = EducationalForm,
- ID_Room = ID_Room,
- id_user = user_id
- };
- }
- }
- <div id="createBlock" float="right" display="inline-block" margin="20px">
- <h3>Добавление </h3>
- <table>
- <tr><td><label>ФИО: </label></td><td><input type="text" id="editFIO" /></td></tr>
- <tr><td><label>Дата Рождения: </label></td><td><input type="text" id="editBirthDate" /></td></tr>
- <tr><td><label>Факультет: </label></td><td><input type="text" id="editFaculty" /></td></tr>
- <tr><td><label>Группа: </label></td><td><input type="text" id="editGroup" /></td></tr>
- <tr><td><label>Форма обучения: </label></td><td><input type="text" id="editEduForm" /></td></tr>
- <tr><td><label>Номер комнаты: </label></td><td><input type="number" id="editNumberRoom" /></td></tr>
- </table>
- <button id="addStudent">Сохранить</button>
- </div>
- <script type="text/javascript">
- $(document).ready(function () {
- GetAllStudents();
- $("#addStudent").click(function (event) {
- event.preventDefault();
- AddStudent();
- });
- });
- function AddStudent() {
- var student = {
- ID_Student: $('#editId').val(),
- FIOStudent: $('#editFIO').val(),
- BirthDate: $('#editBirthDate').val(),
- Faculty: $('#editFaculty').val(),
- Group: $('#editGroup').val(),
- EducationalForm: $('#editEduForm').val(),
- ID_Room: $('#editNumberRoom').val()
- };
- $.ajax({
- url: '/odata/Student',
- type: 'POST',
- data: JSON.stringify(student),
- contentType: "application/json;charset=utf-8",
- success: function (data) {
- GetAllStudents();
- },
- error: function (x, y, z) {
- alert(x + 'n' + y + 'n' + z);
- }
- });
- }
- function GetAllStudents() {
- $("#createBlock").css('display', 'block');
- $.ajax({
- url: '/odata/Student',
- type: 'Get',
- dataType: 'JSON',
- contentType: "application/json;charset=utf-8",
- async: false,
- success: function (data) {
- WriteResponse(data.value);
- },
- error: function (x, y, z) {
- alert(x + 'n' + y + 'n' + z);
- }
- });
- }
- </script>
- ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
- modelBuilder.EntitySet<Student>("Student");
- modelBuilder.EntitySet<StudentDTO>("StudentDTO");
- modelBuilder.EntitySet<StudentDTO>("Student");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement