【发布时间】:2011-01-15 18:07:31
【问题描述】:
我有一个这样的视图模型:
public class SignUpViewModel
{
[Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")]
[DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")]
public bool AgreesWithTerms { get; set; }
}
视图标记代码:
<%= Html.CheckBoxFor(m => m.AgreesWithTerms) %>
<%= Html.LabelFor(m => m.AgreesWithTerms)%>
结果:
不执行验证。到目前为止没关系,因为 bool 是一种值类型,并且永远不会为空。但即使我让 AgreesWithTerms 为空,它也不会工作,因为编译器会喊
“模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。”
那么,处理这个问题的正确方法是什么?
【问题讨论】:
标签: asp.net-mvc validation checkbox data-annotations