Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • webangelo 107 posts 190 karma points
    Jul 18, 2011 @ 17:33
    webangelo
    0

    Razor - Textstring evaluated as DateTime (sometimes)

    I have a field which I use to store the Hours for an event.  As it may list just a starting time or a range I used a separate Text field instead of a DatePicker with Time.  When trying to pull the field in Razor, if it just has a starting time (e.g. 8am) it treats it like a DateTime field with today's date and that time.  If it has a range (i.e. 8am - 4pm) it treats it like a textstring (as I desire).  I currently have to account for this with the following code:

    @(item.meetingTime.GetType() == typeof(DateTime)? @item.meetingTime.ToShortTimeString() : @item.meetingTime)

    What I want to know is this a feature, a bug or a little of both?

    Thanks.

    --Chris

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 18, 2011 @ 17:41
    Sebastiaan Janssen
    1

    It's a feature called ducktyping ("if it walks like a duck and it talks like a duck, it must be a duck"). So if something looks like a datetime, it must be one. 

    You can overcome this by doing: @item.GetProperty("meetingTime").Value 

    The Value of the GetProperty method always returns a string.

  • webangelo 107 posts 190 karma points
    Jul 18, 2011 @ 18:05
    webangelo
    0

    Thanks for the workaround and the info.  Definitely something to be aware of when using Razor.

    --Chris

Please Sign in or register to post replies

Write your reply to:

Draft