简体   繁体   English

Razor DisplayFor of DateTime =输入字符串的格式不正确

[英]Razor DisplayFor of DateTime = Input string was not in a correct format

Recently I updated several of the referenced assemblies for my MVC 4 app and upgraded to MVC 5.1 as well. 最近,我为我的MVC 4应用程序更新了一些引用的程序集,并也升级到了MVC 5.1。 Not sure if this has anything to do with it. 不知道这是否与它有关。

Model 模型

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

Now I am getting a strange error on a simple razor html helper 现在我在一个简单的razor html helper上遇到一个奇怪的错误

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

Gives the error: Input string was not in a correct format . 出现错误: Input string was not in a correct format

If you look at the value of that field in the debugger, it is of type System.DateTime and shows: 2/28/2014 10:17 AM 如果您在调试器中查看该字段的值,则其类型为System.DateTime并显示: 2/28/2014 10:17 AM

Changing to a @Html.EditorFor renders the data time correctly. 更改为@Html.EditorFor正确呈现数据时间。

My DisplayTemplates folder is empty. 我的DisplayTemplates文件夹为空。

Stack: 堆:

[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

Try specifying the DisplayFormat attribute, for example (on your model property): 尝试指定DisplayFormat属性,例如(在您的model属性上):

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

Edit based on edited question: 根据已编辑的问题进行编辑:

Your syntax in your display format is incorrect - it's missing a colon. 您显示格式的语法不正确-缺少冒号。 Do this (notice the colon between the 0 and t ): 这样做(注意0t之间的冒号):

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

Did something stupid. 做了些蠢事。 Some of my [Display(Name = "Start")] had a colon : after the name to render it that way on a form. 我的某些[Display(Name = "Start")]带有一个冒号:在名称之后,以这种方式在表单上呈现。 I used a replace in Visual Studio to replace the colons with "" which also removed the colons between the {0:t} formatstring annotations. 我在Visual Studio中使用了替换,以“”代替冒号,这也删除了{0:t}格式字符串注释之间的冒号。 [DisplayFormat(DataFormatString = "{0t}")] . [DisplayFormat(DataFormatString = "{0t}")] Sorry to waste people's time for my stupidity! 抱歉浪费我的时间让我愚蠢! Thanks all for the responses. 谢谢大家的答复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM