簡體   English   中英

沒有[required]屬性的Razor驗證觸發

[英]Razor validation firing without [required] attribute

這是我的模特

public class SchoolUser
{
    public int schoolId { get; set; }

    public int contact1UserId { get; set; }

    public IEnumerable<DataLayer.salutations> Salutations { get; set; }

    public int departmentId { get; set; }

    public IEnumerable<DataLayer.departments> Departments { get; set; }

    [Required]
    public int roleId { get; set; }

    public IEnumerable<DataLayer.rolesView> roles { get; set; }

這是我的剃刀代碼,用於為deptartmentid視圖創建下拉菜單

                <div style="width:75%; display:inline-block; margin-left:5px;">
                    @Html.DropDownListFor(model => model.departmentId, new SelectList(Model.Departments, "deptId", "description"), "")
                    @Html.ValidationMessageFor(model => model.departmentId)
                </div>                    

令人困惑的是,當沒有選擇部門時,剃須刀驗證就會抱怨。 但是學校模型沒有[必需]屬性。

“ departmentId字段為必填。”

看頁面源,我看到了

 <select data-val="true" data-val-number="The field departmentId must be a number." data-val-required="The departmentId field is required." id="departmentId" name="departmentId"><option value=""></option>
<option value="30">English</option>
</select>

由於某種原因,即使模型沒有[required]或其他約束屬性,剃刀視圖引擎也已“決定”要求填充此字段。

它不能為空。 您應該將其更改為int嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM