1

这是我的模型

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] 或其他约束属性,razor 视图引擎也已“决定”要求填充此字段。

4

1 回答 1

10

它不能为空。你应该把它改成int?.

于 2013-01-18T16:55:19.710 回答