public IActionResult Post([FromBody] Student 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.Entry(obj.Course).State = EntityState.Unchanged;
                _ContosoDbContext.Entry(obj).State        = EntityState.Added;
                //_ContosoDbContext.dbStudent.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, errorresult));
            }
        }
 public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,MiddleName,Email,City,DateBirth,State,ZipCode,Phone,CreatedDate,UpdatedDate,CreatedBy,UpdatedBy,EnrollmentDate")] Student student)
 {
     if (ModelState.IsValid)
     {
         db.Entry(student).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(student));
 }
 public ActionResult Edit([Bind(Include = "Id,Name,Budget,StartDate,InstructorId,RowVersion,CreatedDate,CreatedBy,UpdatedDate,UpdatedBy")] Departments departments)
 {
     if (ModelState.IsValid)
     {
         db.Entry(departments).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.InstructorId = new SelectList(db.People, "Id", "FirstName", departments.InstructorId);
     return(View(departments));
 }
示例#4
0
 public void Atualizar(T obj)
 {
     try
     {
         _contexto.Entry(obj).State = EntityState.Modified;
         _contexto.SaveChanges();
     }
     catch (Exception e)
     {
         throw e;
     }
 }