2014-02-28 100 views
2

最近我更新了幾個我的MVC 4應用程序的引用程序集,並且升級到了MVC 5.1。不知道這與它有什麼關係。Razor DisplayFor的DateTime =輸入字符串的格式不正確

型號

[Display(Name = "Start")] 
[DisplayFormat(DataFormatString = "{0t}")] 
public System.DateTime StartDateTime { get; set; } 

現在我在一個簡單的剃​​刀HTML輔助

@Html.DisplayFor(model => model.StartDateTime) 

得到一個奇怪的錯誤提供了錯誤:Input string was not in a correct format

如果你看的那場在調試器的價值,它的類型是System.DateTime,它和表演:2/28/2014 10:17 AM

正確更改爲@Html.EditorFor呈現數據的時間。

我的DisplayTemplates文件夾爲空。

堆棧:

[FormatException: Input string was not in a correct format.] 
    System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) +14321177 
    System.String.Format(IFormatProvider provider, String format, Object[] args) +136 
    System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +775 
    System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +241 
    System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +332 
    System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +333 
    System.Web.Mvc.Html.DisplayExtensions.DisplayFor(HtmlHelper`1 html, Expression`1 expression) +151 
    ASP._Page_Areas_Mobile_Views_LogTimeForPart_CurrentTask_cshtml.Execute() in c:\Users\Chad\documents\Incite Systems\clients\hh metals\20 - development\otis\app\otis.web\Areas\Mobile\Views\LogTimeForPart\CurrentTask.cshtml:41 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +392 
    System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +165 
    System.Web.WebPages.StartPage.RunPage() +77 
    System.Web.WebPages.StartPage.ExecutePageHierarchy() +123 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +212 
    System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +1161 
    System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +639 
    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +828 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +65 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +178 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +1131 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +181 
    System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +549 
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +330 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +58 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +90 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68 
    System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +60 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +85 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +58 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +60 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +93 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +58 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +59 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288 
+0

您的模型看起來如何? – CodeMad

+0

@CodeMad是的,這將是很好的添加到問題:)看到更新。 –

回答

3

嘗試指定DisplayFormat屬性,例如(在你的模型屬性):

[DisplayFormat(DataFormatString = "{0:C}")] 
public DateTime StartDateTime { get; set; } 

編輯根據編輯的問題:

你在你的顯示器語法格式不正確 - 它缺少冒號。做到這一點(注意0t之間的冒號):

[Display(Name = "Start")] 
[DisplayFormat(DataFormatString = "{0:t}")] 
public System.DateTime StartDateTime { get; set; } 
+0

對不起,我將模型添加到問題中。需要格式化以顯示時間。所以我有[DisplayFormat(DataFormatString =「{0t}」)] –

+0

看到我的回答...剛纔意識到我做了一個愚蠢的查找/替換我的源代碼模型,它消滅了冒號.... :( –

1

做了一件愚蠢的。我的一些[Display(Name = "Start")]有一個冒號:在名稱後面以表格方式渲染它。我在Visual Studio中使用替換來替換冒號「」,這也刪除了{0:t}格式字符串註釋之間的冒號。 [DisplayFormat(DataFormatString = "{0t}")]。對不起,浪費人們的時間,我的愚蠢!感謝所有的答覆。

+1

它導致我修復了我的代碼,所以我至少感激它! – TomDestry