System.NullReferenceException: 'Object reference not set as an instance of an object. When trying to implement a POST method in .net

0

This is my code:

public IHttpActionResult PostNewStudent(StudentViewModel student)
{
            if (!ModelState.IsValid)
                return BadRequest("Invalid data.");

            using (var ctx = new SchoolEntities())
            {

                ctx.Students.Add(new Student()
                {
                    StudentId = student.Id,
                    FirstName = student.FirstName,
                    LastName = student.LastName
                });

                ctx.SaveChanges();
            }

            return Ok();
}

But when compiling it, I get this warning and it bursts. How should I organize my code so that I do not get this uncontrolled exception?

    
asked by jmtaborda 30.03.2018 в 05:19
source

0 answers