Pete Field
Pete Field

Reputation: 484

Rendering a Date Field in Umbraco 5

I'm a total newbie when it comes to Umbraco, and I'm trying to set up a very simple template to get started with.

I'm using Umbraco 5 and have created a document type and added a 'Date' property to it. The propery is of type Date Time Picker.

I then created a Template and inserted an umbraco page field ie @Umbraco.Field("Date")

When I preview a content item based on the above document type, get the following error

Unable to cast object of type 'System.DateTimeOffset' to type 'System.String'.

Stack Trace:

[InvalidCastException: Unable to cast object of type 'System.DateTimeOffset' to type 'System.String'.]
Umbraco.Cms.Web.FieldRenderer.RenderField(IRoutableRequestContext routableRequestContext, ControllerContext controllerContext, Content item, String fieldAlias, String valueAlias, String altFieldAlias, String altValueAlias, String altText, String insertBefore, String insertAfter, Boolean recursive, Boolean convertLineBreaks, Boolean removeParagraphTags, UmbracoRenderItemCaseType casing, UmbracoRenderItemEncodingType encoding) +464
Umbraco.Cms.Web.UmbracoHelper.Field(Content currentPage, String fieldAlias, String valueAlias, String altFieldAlias, String altValueAlias, String altText, String insertBefore, String insertAfter, Boolean recursive, Boolean convertLineBreaks, Boolean removeParagraphTags, UmbracoRenderItemCaseType casing, UmbracoRenderItemEncodingType encoding) +187
Umbraco.Cms.Web.UmbracoHelper.Field(String fieldAlias, String valueAlias, String altFieldAlias, String altValueAlias, String altText, String insertBefore, String insertAfter, Boolean recursive, Boolean convertLineBreaks, Boolean removeParagraphTags, UmbracoRenderItemCaseType casing, UmbracoRenderItemEncodingType encoding) +137 ASP._Page_Views_Umbraco_blogPost_cshtml.Execute() in c:\inetpub\wwwroot\Umbraco\Views\Umbraco\blogPost.cshtml:14
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +157
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378 System.Web.Mvc.<>c_DisplayClass1c.b_19() +33 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +728112 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +728112 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +728068
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334 System.Web.Mvc.<>c_DisplayClassb.b_5() +62 System.Web.Mvc.Async.<>c_DisplayClass1.b_0() +15
System.Web.Mvc.<>c_DisplayClasse.b_d() +52
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +514 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +354

What on earth am I doing wrong!

Any help gratefully received!

Cheers

Pete

Upvotes: 0

Views: 804

Answers (1)

Warren Buckley
Warren Buckley

Reputation: 1431

I would recommend you retrieve the field like so in your view/template

@DynamicModel.Date

or you could use Model @Model.Field("Date")

By the way to ensure you get better responses, I would post your questions over at the our.umbraco.org community forums

Upvotes: 3

Related Questions