public ActionResult Create([Bind(Include = "StudentID,ClassID,FullName,Gender,Birthday,Address,Email,PhoneNumber")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ClassID = new SelectList(db.Classes, "ClassID", "Period", student.ClassID);
            return(View(student));
        }
예제 #2
0
        public HttpResponseMessage EditStudent(string id, [FromBody] Student student)
        {
            try
            {
                using (ECLASSEntities db = new ECLASSEntities())
                {
                    var s = db.Students.SingleOrDefault(x => x.StudentID == id);
                    if (s != null)
                    {
                        s.FullName = student.FullName;
                        s.ClassID  = student.ClassID;
                        s.Birthday = student.Birthday;
                        s.Address  = student.Address;
                        s.Email    = student.Email;
                        s.Gender   = student.Gender;

                        db.SaveChanges();
                    }
                    return(Request.CreateResponse(HttpStatusCode.OK, s));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
예제 #3
0
        public HttpResponseMessage AddStudent([FromBody] Student student)
        {
            try
            {
                using (ECLASSEntities db = new ECLASSEntities())
                {
                    db.Students.Add(student);
                    db.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.Created));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
예제 #4
0
 public HttpResponseMessage DeleteStudent(string id)
 {
     try
     {
         using (ECLASSEntities db = new ECLASSEntities())
         {
             Student s = db.Students.SingleOrDefault(x => x.StudentID == id);
             db.Students.Remove(s);
             db.SaveChanges();
             return(Request.CreateResponse(HttpStatusCode.OK, s));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
     }
 }