“The Id field is required” validation message on Create; Id not set to [Required]

前端 未结 16 1423
盖世英雄少女心
盖世英雄少女心 2020-12-07 17:18

This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2.

The POCO has the following properties:

public int Id          


        
16条回答
  •  死守一世寂寞
    2020-12-07 18:08

    I ran into this issue with a form in which I was adding "objects" to a list dynamically. Therefore, our users were able to add, remove or update. It all worked well except for the cases when new items were created. For this reason, in my case, excluding the Id property was not an option. The solution was to make the ID Nullable:

    public int? Id { get; set; }
    

    This way existing items will have a value and new ones will have null instead. Good stuff.

提交回复
热议问题