asp.net - ASP.NET MVC 3 中 DropDownListFor 的不显眼验证问题

标签 asp.net asp.net-mvc entity-framework-4 asp.net-mvc-3 unobtrusive-validation

为什么即使我没有在模型中指定 [Required] 属性,也会触发客户端验证说我的默认主题字段是必需的?

enter image description here

模型 :

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。

最佳答案

那是因为 int 有一个隐含的值。如果您希望它没有值,请使用 int?

关于asp.net - ASP.NET MVC 3 中 DropDownListFor 的不显眼验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5011033/

相关文章:

asp.net - 将单选按钮与相应的标签对齐

asp.net-mvc - Elmah 基本设置问题/问题

c# - LINQ Order By Descending with Null Values on Bottom

.net - Entity Framework : Type "mapped as a complex type" Error

c# - DbContext 不释放 SQLite 数据库

c# - 在新构建后保留 Session[] 变量

c# - 结果显示 System.String[]

c# - 在 context.Request 中解码查询字符串

asp.net - 如何以编程方式设置 Elmah 电子邮件的收件人

linq - Linq 的奇怪行为