【发布时间】: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] 或其他约束属性。
【问题讨论】:
-
@TimBJames,您链接中的问题与我遇到的问题无关。
-
3 个月后,某个获胜者出现并匿名狙击我。一定是什么无名小卒,什么都知道! :)
-
+1 为您备份 :)
标签: asp.net-mvc asp.net-mvc-3 razor razor-declarative-helpers