EditorFor on nullable DateTime - “Nullable object must have a value.”

前端 未结 3 1127
醉梦人生
醉梦人生 2021-01-03 09:43

I am attempting to display a form that allows a user to input a new assignment for a person. I\'m using a DateTime.cshtml EditorTemplate to handle DateTime values for the a

3条回答
  •  天命终不由人
    2021-01-03 09:58

    create a DateTime.cshtml in your Shared/DisplayTemplate folder

    @model Nullable
    @(Model != null ? string.Format(ViewData.ModelMetadata.DisplayFormatString ?? "{0:d}", Model) : string.Empty)
    

    this supports metadata from datannotations to be used if found.

提交回复
热议问题