gpt4 book ai didi

asp.net - MVC3 删除了 "required"但不断获取 data-val-required 属性

转载 作者:行者123 更新时间:2023-12-02 17:48:40 31 4
gpt4 key购买 nike

我的模型

    [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;
.....
}

我的模型中没有必需的属性,但我仍然得到这个

enter image description here

我指的是This删除必填字段,但它不适用于我的代码。帮我删除必填字段。

最佳答案

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

    [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;

关于asp.net - MVC3 删除了 "required"但不断获取 data-val-required 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14452344/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com