空数据列 returns 输入字符串格式不正确错误

Null data column returns input string was not in a correct format error

我正在制作一个事件日历,其中时间值(开始、结束)并不总是必需的,而且通常是 Null。我想要 的时间格式,以便阅读 hh:mm tt。我使用以下 if 语句检查空值的代码:

<td>
    @If item.StartTime IsNot Nothing Then
        @item.StartTime.ToString("h:mm tt")
    Else
        @Html.DisplayFor(Function(modelItem) item.StartTime)
    End If
</td>

仍然得到以下错误尽管if声明:

[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +224
Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +92

[InvalidCastException: Conversion from string "h:mm tt" to type 'Integer' is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +248
ASP._Page_Views_EaglesEvents_Index_vbhtml.Execute() in C:\Users\Iluvw\Documents\Visual Studio 2017\Projects\MVC Projects\Eagles3555\Eagles3555\Views\EaglesEvents\Index.vbhtml:49
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
System.Web.WebPages.StartPage.RunPage() +17
System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.<>c__DisplayClass7_0.<InvokeEndHandler>b__0() +32
System.Web.StepInvoker.Invoke(Action executionStep) +107
System.Web.<>c__DisplayClass4_0.<Invoke>b__0() +23
Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule.OnExecuteRequestStep(HttpContextBase context, Action step) +64
System.Web.<>c__DisplayClass284_0.<OnExecuteRequestStep>b__0(Action nextStepAction) +56
System.Web.StepInvoker.Invoke(Action executionStep) +91
System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +123
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +128

以红色突出显示的行是 @item.StartTime.ToString("h:mm tt")。我做错了什么?

编辑: 我的时间字段模型代码:

<Display(Name:="Date")>
<DisplayFormat(DataFormatString:="{0:yyyy-MM-dd}", ApplyFormatInEditMode:=True)>
<DataType(DataType.Date)>
Public Property EventDate As DateTime
<Display(Name:="Start Time")>
<DisplayFormat(DataFormatString:="{0:hh:mm:ss}", ApplyFormatInEditMode:=True)>
<DataType(DataType.Time)>
Public Property StartTime As DateTime?

出现异常的原因是您使用的 Nullable DateTime 没有接受格式字符串的 ToString() 方法。如果您首先进行空检查,那么您可以通过对 Nullable 的 Value 执行 ToString() 来解决此问题,这将是 DateTime:

@If item.StartTime IsNot Nothing Then
        @item.StartTime.Value.ToString("h:mm tt")
// ...