2014-11-02 116 views
1

我認爲在提交表單之前需要填寫以下字段。MVC驗證 - 非必填字段在提交表單前被強制填寫

VIEW:

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

MODEL:

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

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

[DisplayName("Mobile Phone")] 

這些字段標在我的數據庫爲 「允許空值」,並沒有[必需]數據標註在我的模型。爲什麼在提交表格之前需要他們?

回答

2

也許嘗試使用可爲空的電話號碼?

如:

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

真棒!這很好。不幸的是,我還沒有15分給你一個觀點:( – dc922 2014-11-03 00:06:53

+0

你可以標記爲正確的答案 - 我想你甚至可能得到一些點? – iceburg 2014-11-03 04:40:34