Jeff Tung
Jeff Tung

Reputation: 99

ModelState isvalid return false when id / key = 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?

Upvotes: 0

Views: 751

Answers (1)

Michael Roy Somera
Michael Roy Somera

Reputation: 39

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);
    }
 }

Upvotes: 1

Related Questions