Unobtrusive validation issue with DropDownListFor in ASP.NET MVC 3

Why does a client validation error occur saying that the Default Subject field is required even if I did not specify the [Required] attribute in my model?

enter image description here

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 entity mapping with POCO in my model.

+4
source share
1 answer

This is because ints has an implicit value. If you want it not to matter, change the type to nullable int using int?

+7
source

Source: https://habr.com/ru/post/1339915/


All Articles