ASP.NET MVC optional field being treated as required

前端 未结 6 2016
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 02:57

I have this field that for some reason when I click on submit, gets a validation message that the field is required.

[DisplayName(\"Total Budget:\")]
public          


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 03:52

    The built-in DefaultModelBinder in MVC will perform required and data type validation on value types like int, DateTime, decimal, etc. This will happen even if you don't explicitly specify validation using someting like [Required].

    In order to make this optional, you will have to define it as nullable:

    public double? Budget { get; set; }
    

提交回复
热议问题