Esempio n. 1
0
        public IActionResult Post([FromBody] Course obj)
        {
            //Check validations
            //Check the data if validation is ok
            var context = new ValidationContext(obj, null, null);

            //Store the list of errors
            var errorresult = new List <ValidationResult>();

            //Check if the object is valid
            var isValid = Validator.TryValidateObject(obj, context, errorresult, true);

            if (isValid)
            {
                _ContosoDbContext.dbCourse.Add(obj);
                _ContosoDbContext.SaveChanges();

                //This action needs to return JSON file, so use the return JSON to convert the object to JSON
                return(StatusCode(StatusCodes.Status200OK, obj));
            }
            else
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, obj));
            }
        }
        public ActionResult Create([Bind(Include = "Id,FirstName,LastName,MiddleName,Email,City,DateBirth,State,ZipCode,Phone,CreatedDate,UpdatedDate,CreatedBy,UpdatedBy,EnrollmentDate")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.People.Add(student);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(student));
        }
Esempio n. 3
0
 public void Adicionar(T obj)
 {
     try
     {
         _contexto.Set <T>().Add(obj);
         _contexto.SaveChanges();
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        public ActionResult Create([Bind(Include = "Id,Name,Budget,StartDate,InstructorId,RowVersion,CreatedDate,CreatedBy,UpdatedDate,UpdatedBy")] Departments departments)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(departments);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.InstructorId = new SelectList(db.People, "Id", "FirstName", departments.InstructorId);
            return(View(departments));
        }
 public IHttpActionResult PostStudent([FromBody] Student student)
 {
     try
     {
         var Students = db.Students.ToList();
         if (Students == null) //in case that there aren't any students on the db yet
         {
             Students = new List <Student>();
         }
         if (Students.Exists(s => s.Id == student.Id)) //validates if the id written it's already on the db
         {
             return(NotFound());                       //return response id it's incorrect
         }
         else
         {
             db.Students.Add(student);
         }
         db.SaveChanges();
         return(Ok());
     }
     catch
     {
         return(InternalServerError()); //return response when there is an error adding an student
     }
 }