コード例 #1
0
ファイル: TeachersController.cs プロジェクト: Aleks-g3/test
        public IHttpActionResult PutTeacher(int id, Teacher teacher)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != teacher.Id)
            {
                return(BadRequest());
            }

            db.Entry(teacher).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TeacherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
 public ActionResult Create([Bind(Include = "StudentsID,CoursesID")] StudentCours stuc)
 {
     if (ModelState.IsValid)
     {
         db.StudentCourses.Add(stuc);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(stuc));
 }
コード例 #3
0
 public ActionResult Create([Bind(Include = "StudeneID,StudentName")] student students)
 {
     if (ModelState.IsValid)
     {
         db.students.Add(students);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(students));
 }
コード例 #4
0
        public ActionResult Create([Bind(Include = "CoursesID,CourseName,CourseDesc")] Cours course)
        {
            if (ModelState.IsValid)
            {
                db.Courses.Add(course);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(course));
        }
コード例 #5
0
        //Deletes student data from database

        public IHttpActionResult Delete(int id)
        {
            if (id <= 0)
            {
                return(BadRequest("Not a valid student Id"));
            }

            using (var ctx = new SchoolEntities1())
            {
                var existingStudent = ctx.Students
                                      .Where(s => s.StudentID == id)
                                      .FirstOrDefault();
                if (existingStudent != null)
                {
                    ctx.Students.Remove(existingStudent);
                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
コード例 #6
0
        //Updates the existing data in the database

        public IHttpActionResult PutStudent(StudentViewModel studentAddress)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data"));
            }

            using (var ctx = new SchoolEntities1())
            {
                var existingStudent = ctx.Students
                                      .Where(s => s.StudentID == studentAddress.Id)
                                      .FirstOrDefault();

                if (existingStudent != null)
                {
                    existingStudent.StudentName             = studentAddress.StudentName;
                    existingStudent.StandardId              = studentAddress.stardardId;
                    existingStudent.StudentAddress.Address1 = studentAddress.Address.Address1;
                    existingStudent.StudentAddress.Address2 = studentAddress.Address.Address2;
                    existingStudent.StudentAddress.City     = studentAddress.Address.City;
                    existingStudent.StudentAddress.State    = studentAddress.Address.State;

                    ctx.SaveChanges();
                }

                else
                {
                    NotFound();
                }
            }

            return(Ok());
        }
コード例 #7
0
        //Posts new student in the datbase

        public IHttpActionResult PostNewStudent(StudentViewModel studentAddress)
        {
            //This will make sure that the student object includes all the necessary information. If it is not valid, it will return BadRequest response.
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data"));
            }

            using (var ctx = new SchoolEntities1())
            {
                ctx.Students.Add(new Student()
                {
                    StudentID   = studentAddress.Id,
                    StudentName = studentAddress.StudentName,
                    StandardId  = studentAddress.stardardId,
                });

                ctx.StudentAddresses.Add(new StudentAddress()
                {
                    StudentID = studentAddress.Address.Id,
                    Address1  = studentAddress.Address.Address1,
                    Address2  = studentAddress.Address.Address2,
                    City      = studentAddress.Address.City,
                    State     = studentAddress.Address.State,
                });
                ctx.SaveChanges();
            }

            return(Ok());
        }