C# 我的可为Null的DateTime视图失败,返回Null

C# 我的可为Null的DateTime视图失败,返回Null,c#,asp.net-mvc,datetime,C#,Asp.net Mvc,Datetime,在我的MVC应用程序中,我有一个共享的DateTime.cshtml视图,定义如下: @model DateTime? @{ var value = ""; if (Model.HasValue) { value = String.Format("{0:d}", Model.Value); } } @Html.TextBox("", value, new { @class = "form-control datepicker", type = "

在我的MVC应用程序中,我有一个共享的
DateTime.cshtml
视图,定义如下:

@model DateTime?
@{
    var value = "";
    if (Model.HasValue) 
    {
        value = String.Format("{0:d}", Model.Value);
    }
}
@Html.TextBox("", value, new { @class = "form-control datepicker", type = "text" })
@model PublicationSystem.ViewModels.ProfileEdit
<div class="form-group">
    @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.BirthDate)
    </div>
</div>
[DataType(DataType.Date), Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDate { get; set; }
在我的一个观点中,我是这样使用它的:

@model DateTime?
@{
    var value = "";
    if (Model.HasValue) 
    {
        value = String.Format("{0:d}", Model.Value);
    }
}
@Html.TextBox("", value, new { @class = "form-control datepicker", type = "text" })
@model PublicationSystem.ViewModels.ProfileEdit
<div class="form-group">
    @Html.LabelFor(model => model.BirthDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.BirthDate)
    </div>
</div>
[DataType(DataType.Date), Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDate { get; set; }
我如何让它加载视图,即使那里有一个空值?我做错了什么

顺便说一句,我读过其他类似的文章,大多数都说让编辑器控件的模型为空,但是正如你所看到的,我已经做过了

更新:

以下是控制器操作(Mapper.Map只是获取模型,并填充ViewModel
ProfileEdit
):

[CheckId(redirectoreferer=true]
公共虚拟操作结果编辑(Guid id)
{
var profileEdit=Mapper.Map(db.Profile.Find(id));
if(profileEdit==null)
{
返回HttpNotFound();
}
返回视图(profileEdit);
}

@Mangist是对的。正是这种属性:

[DataType(DataType.Date), Display(Name = "Birth Date")]

特别是数据类型,因为它是
Date
而不是
DateTime
,就像我的模板一样。

“传递到字典中的模型项为null,但此字典需要类型为'System.DateTime'的非null模型项。”字典在哪里声明,如何声明?我不知道。也许MVC正在使用其他东西来呈现编辑器。我测试了你的模板,它工作正常,没有任何错误。显示您的控件操作发送视图的控件操作您是否尝试删除BirthDate属性上的这些属性,并查看是否查看渲染时没有错误?模板可以是
@model DateTime@Html.TextBox(“,”{0:d}),new{@class=“form control datepicker”})
(其余部分不需要)。如果您收到该错误,这是因为您正在调用另一个视图/模板,而不是您显示的视图/模板