【问题标题】:Razor validation firing without [required] attribute没有 [required] 属性的 Razor 验证触发
【发布时间】:2013-01-18 16:52:27
【问题描述】:

这是我的模型

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 视图创建下拉菜单的 razor 代码

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

由于某种原因,razor 视图引擎已“决定”要求填充此字段,即使模型没有 [required] 或其他约束属性。

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-3 razor razor-declarative-helpers


【解决方案1】:

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

【讨论】:

  • 当然,现在想来,如果整数不能为空,它就不能为空!!!! @Zote,你摇滚!
  • +1 简单但真实 - 记得几年前有同样的头在挠。
猜你喜欢
  • 2014-06-11
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多