ASP.NET MVC 3 中 DropDownListFor 的不引人注目的验证问题
即使我没有在模型中指定 [Required] 属性,为什么会触发客户端验证并指出我的默认主题字段是必需的?
模型:
public class Site
{
public int SiteId { get; set; }
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("Title")]
public string Title { get; set; }
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("RootDirectory")]
public string RootDirectory { get; set; }
[LocalizedDisplayName("DefaultTheme")]
public int DefaultThemeId { get; set; } // <-- No required attribute here
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("ThemesDirectory")]
public string ThemesDirectory { get; set; }
public virtual Theme DefaultTheme { get; set; } // <-- No required attribute here
}
视图:
@Html.DropDownListFor(x => x.DefaultThemeId,
new SelectList(ViewBag.Themes, "ThemeId", "Name"), string.Empty)
我正在使用 Entity Framework 4 和 ADO.NET 实体数据模型,并将实体映射到我的模型中的 POCO。
Why is it that client validation is getting triggered saying that my Default Theme field is required even if I didn't specify a [Required] attribute in my model?
Model :
public class Site
{
public int SiteId { get; set; }
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("Title")]
public string Title { get; set; }
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("RootDirectory")]
public string RootDirectory { get; set; }
[LocalizedDisplayName("DefaultTheme")]
public int DefaultThemeId { get; set; } // <-- No required attribute here
[Required(ErrorMessage = "*")]
[LocalizedDisplayName("ThemesDirectory")]
public string ThemesDirectory { get; set; }
public virtual Theme DefaultTheme { get; set; } // <-- No required attribute here
}
View :
@Html.DropDownListFor(x => x.DefaultThemeId,
new SelectList(ViewBag.Themes, "ThemeId", "Name"), string.Empty)
I am using Entity Framework 4 with ADO.NET Entity Data Model and mapping the entities to POCOs in my model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为整数有一个隐含的值。如果您希望它没有值,请使用 int? 将类型更改为可空 int?
That's because ints have an implicit value. If you want it to have no value, change the type to a nullable int using int?