MVC验证 - 在提交表单之前强制填写非必填字段

时间:2014-11-02 23:56:34

标签: asp.net-mvc data-annotations

我在视图中有以下字段,在提交表单之前需要填写这些字段。

查看:

<div class="col-md-4">
    @Html.EditorFor(model => model.BusinessPhone, new { htmlAttributes = new { @class = "form-control boldgray", @placeholder = "Business Phone" } })
</div>

<div class="col-md-4">
    @Html.EditorFor(model => model.MobilePhone, new { htmlAttributes = new { @class = "form-control boldgray", @placeholder = "Mobile/Alternative" } })
</div>

模特:

[DisplayName("Home Phone")]
[Required]
public int HomePhone { get; set; }

[DisplayName("Business Phone")]
public int BusinessPhone { get; set; }

[DisplayName("Mobile Phone")]

这些字段标记为&#34; Allow Null&#34;在我的数据库中,我的模型中没有[Required]数据注释。为什么在提交表单之前需要他们?

1 个答案:

答案 0 :(得分:2)

也许可以尝试使用可空的整数来获取电话号码?

例如:

 [DisplayName("Business Phone")]
        public int? BusinessPhone { get; set; }