0

ModelState Isvalid is returning false when Primary Key is 0 on Creation.

In model:

[Key]
        public int CourseId { get; set; }

In database:

enter image description here

database data:

enter image description here

In View:

enter image description here

Value is 0: enter image description here

ModelState IsValid return false:

enter image description here

Any idea why is it invalid?

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Jeff Tung
  • 99
  • 2
  • 10

1 Answers1

1

Try this approach. Don't return a null value and instead return a new instance of Course

  public ViewResult AddCourse(int id)
  {
        var course = yourservice.GetById(id);

        if(customer == null)
        {
            course = new Course();
        }
        return View(course);
    }
 }