Reputation: 1863
I am trying to render the following in a MVC view with a nullable DateTime(DateTime?), but I got "System.InvalidOperationException: Nullable object must have a value". When the value is not null, show the time part (13:15:00) of the date time string (01/01/2014 13:15:00). What is happening? Thanks.
@Html.EditorFor(model => model.Task.Scheduled_Time.Value, "{0:t}")
Upvotes: 0
Views: 2169
Reputation: 16348
Remove the .Value
.
When using the HTML helper methods you need to tell the helper which property you want to use (and what it's type is).
If you say
model => model.Task.Scheduled_Time
You're saying: "Get the model and create an editor for whatever Scheduled_Time is."
If you say
model => model.Task_Scheduled_Time.Value
You're saying: "Get the model and create an editor for the property of the value of Scheduled_Time."
If Scheduled_Time
isn't set (that is: it's null) then .Value
is nothing and so the property you're trying to select isn't really accessible.
Upvotes: 2