开发者

Unobtrusive validation problem with DropDownListFor in ASP.NET MVC 3

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?

Unobtrusive validation problem with DropDownListFor in ASP.NET MVC 3

Model :

public class Site
{
    public int SiteId { get; set; }

    [Required(ErrorMessage = "*")]
    [LocalizedDisplayName("Title")]
    public string Title { get; set; }

    [Required(ErrorMessage = "*")]
    [Localize开发者_开发百科dDisplayName("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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜