In my MVC app, I have a shared DateTime.cshtml view defined like this:
@model DateTime?
@{
var value = "";
if (Model.HasValue)
{
value = String.Format("{0:d}", Model.Value);
}
}
@Html.TextBox("", value, new { @class = "form-control datepicker", type = "text" })
In one of my views I use it like this:
@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>
However, if my model has a Null for BirthDate, I get this message:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
In the model ProfileEdit I have the field defined like this:
[DataType(DataType.Date), Display(Name = "Birth Date")]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? BirthDate { get; set; }
How can I get it to load the view, even if there is a Null there? What am I doing wrong?
