MVC3删除了“必需的”,但仍将继续得到数据-VAL要求的属性(MVC3 removed “requ

2019-08-07 17:52发布

我的模型

    [DataType(DataType.Text)]
    [Display(Name = "Number of Employee")]
    public int NumberOfEmployee { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "CEO Birthday")]
    [DateRange(2)]
    public DateTime CEO_Birthday { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Organisation Anniversary")]
    [DateRange(3)]
    public DateTime OrgAnniversary { get; set; }

查看

          <tr>
            <td style="border:0;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.NumberOfEmployee)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.NumberOfEmployee)
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;vertical-align:middle;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.CEO_Birthday)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.CEO_Birthday, new { @class="date"})
                </div>
            </td>
        </tr>

        <tr>
            <td style="border:0;vertical-align:middle;">
                <div class="editor-label">
                    @Html.LabelFor(m => m.OrgAnniversary)                            
                </div> 
            </td>
            <td style="border:0;">
                <div class="editor-field">
                    @Html.TextBoxFor(m => m.OrgAnniversary, new { @class = "date" })
                </div>
            </td>
          </tr>

的Global.asax.cs

protected void Application_Start()
    {
        DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
       .....
    }

没有必要的财产在我的模型,但仍然我得到这个

我指该删除必填字段,但它为我的代码并不适用。 帮我删除必填字段。

Answer 1:

我设置为空的模型的属性,即得到了我的答案。

    [DataType(DataType.Text)]
    [Display(Name = "Number of Employee")]
    public int? NumberOfEmployee { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "CEO Birthday")]
    [DateRange(2)]
    public DateTime? CEO_Birthday { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Organisation Anniversary")]
    [DateRange(3)]
    public DateTime? OrgAnniversary { get; set; }

无需添加此

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;


文章来源: MVC3 removed “required” but keeps getting data-val-required attribute