Asp.net mvc 编辑日期仅为年

Asp.net mvc 编辑日期仅为年,asp.net-mvc,Asp.net Mvc,我有一个小测验,只编辑日期字段中的“年”部分。 我尝试了一种简单的格式化属性的方法: Partial Public Class Book Public Property BookID As Integer Public Property Title As String Public Property Author As String <DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="

我有一个小测验,只编辑日期字段中的“年”部分。 我尝试了一种简单的格式化属性的方法:

Partial Public Class Book
    Public Property BookID As Integer
    Public Property Title As String
    Public Property Author As String
    <DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0:yyyy}")>
    Public Property Year As Nullable(Of Date)
    Public Property CategoryID As Integer
    Public Property Image As Byte()

    Public Overridable Property Category As Category

End Class
部分公共课堂教材
公共属性BookID为整数
公共财产所有权作为字符串
作为字符串的公共属性作者
公共财产年度可为空(自日期起)
公共属性类别ID为整数
作为字节()的公共属性映像
作为类别的公共可重写属性类别
末级
我的观点是

<div class="editor-field">
    <%: Html.EditorFor(Function(model) model.Year)%>
    <%: Html.ValidationMessageFor(Function(model) model.Year)%>
</div>

但它不起作用。当我在输入字段中填写“2014”并按“提交”时,验证检查器告诉我“2014”不是有效日期。
那么我如何解决这个问题呢?

为什么要将年份存储为日期而不是整数?如果您只需要年份,那么只需将字段设置为
int
,而不是
DateTime
,编译器无法从一年中提取日期,它必须知道日期和月份…您已将年定义为日期类型,但仅提交一个整型值作为年份部分,是否需要将年定义为int,然后验证输入值,或者编写一个接受int但输出日期的自定义模型绑定器?@StuTheDog我认为第二种方法更好。(对每个人)存储整数而不是日期是不可接受的:)为什么不可接受?