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

前端 未结 16 1486
盖世英雄少女心
盖世英雄少女心 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:12

    Great question and answers, saved my ... behind. I need to add something, though:

    Instead of

    [Bind(Exclude = "Id")]
    

    I think it's better to use

    [Bind(Include = "Prop1, Prop2, Prop3, etc")]
    

    .. where Prop1, Prop2 and Prop3 are THE ONLY properties that you want to be bound at the action level.

    Since this is white-listing as opposed to black-listing. White-listing is better, safer. This way you also solve the risk of over posting and under posting too. See Brad Wilson's post.

提交回复
热议问题