Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Jun 07, 2018 @ 12:58
    Matt
    0

    Umbraco field not showing correct data

    Hello,

    Im having an issue with the following Umbraco fields;

    <div class="post-author">@Umbraco.Field("writerName")</div>
    
    
    <div class="blog-post-date">@Umbraco.Field("createDate")</div>
    

    I get a date and an Author but its not the correct data or Author? anyone able to point me in right direction what could be causing this?

    Thanks in advance, Matt

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 07, 2018 @ 13:24
    Nik
    0

    Hi Matt,

    Are you doing this in your template view or a partial view? A recent thread highlighted that you cannot use Umbraco.Field in a partial view (I didn't know this), but I would advise using GetPropertyValue to retrieve values instead of Field.

    Thanks,

    Nik

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 07, 2018 @ 17:14
    Jan Skovgaard
    1

    Hi Nik

    Is it this post by me you're having in mind? Then please read my edit - https://our.umbraco.org/forum/using-umbraco-and-getting-started/92301-change-url-on-image#comment-291932

    I mixed up some concepts so my initial response is not true unfortunately. Sorry about that!

    If you're using a partial you'll need to add some context so the @Umbraco.Field() method knows where to get the data from. So it could be @Umbraco.Field(CurrentPage, "someField) or if you're in a loop and have called your variable "item"...@Umbraco.Field(item, "someField")

    Hope this clears up things.

    /Jan

  • Matt 353 posts 825 karma points
    Jun 07, 2018 @ 15:09
    Matt
    0

    Hello Nik,

    I have 1 in template view and 1 as a partial view, both of them have the same issue.

    I tried GetPropertyValue but then it didn't show anything.

    Thanks in advance, Matt

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 07, 2018 @ 15:35
    Nik
    1

    Hi Matt,

    When you say this:

    I get a date and an Author but its not the correct data or Author? anyone able to point me in right direction what could be causing this?

    What exactly do you mean?

    Can you provide a wider example of your rendering code as I suspect there is context missing that might provide pointers towards the answer for you.

    Thanks,

    Nik

  • Harry Spyrou 212 posts 604 karma points
    Jun 07, 2018 @ 15:38
    Harry Spyrou
    0

    Can you post your whole partial view please?

  • Matt 353 posts 825 karma points
    Jun 08, 2018 @ 09:32
    Matt
    0

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
        var selection = Model.Content.Site().FirstChild("blog").Children("blogItem").OrderBy("CreateDate desc").Take(3);
    }
    
        @foreach(var item in selection){
    
                      <div class="recent-blog-div">
                        <a href="@item.Url" class = "main-page-blog-title-links"><h4 class="recent-blog-title">@item.GetPropertyValue("blogPostShortTitle")</h4></a>
                        <div class="text-block-3">@Umbraco.Field("createDate", formatAsDate: true)</div>
                      </div>
        }
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 08, 2018 @ 09:44
    Nik
    0

    Hey Matt,

    Try this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = Model.Content.Site().FirstChild("blog").Children("blogItem").OrderBy("CreateDate desc").Take(3);
    
        foreach(var item in selection){
          <div class="recent-blog-div">
              <a href="@item.Url" class = "main-page-blog-title-links"><h4 class="recent-blog-title">@item.GetPropertyValue("blogPostShortTitle")</h4></a>
              <div class="text-block-3">@(item.GetPropertyValue<DateTime>("createDate").ToShortDateString())</div>
           </div>
         }
    }
    
  • Matt 353 posts 825 karma points
    Jun 08, 2018 @ 10:30
    Matt
    0

    Hello Nik,

    Thanks for the reply.

    This is what it displays out;

    1/1/0001

  • Harry Spyrou 212 posts 604 karma points
    Jun 08, 2018 @ 13:22
    Harry Spyrou
    0

    Do you actually have a custom field called 'createDate' or are you just trying to get the CreateDate?

  • Matt 353 posts 825 karma points
    Jun 08, 2018 @ 13:32
    Matt
    0

    Hello Harry,

    No custom field, I'm just trying to get the date of which the node was created by.

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 08, 2018 @ 13:57
    Jan Skovgaard
    100

    Hi Matt

    Can you please try this example?

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = Model.Content.Site().FirstChild("blog").Children("blogItem").OrderBy("CreateDate desc").Take(3);
    
        foreach(var item in selection){
          <div class="recent-blog-div">
              <a href="@item.Url" class = "main-page-blog-title-links"><h4 class="recent-blog-title">@item.GetPropertyValue("blogPostShortTitle")</h4></a>
              <div class="text-block-3">@item.CreateDate.ToShortDateString()</div>
           </div>
         }
    }
    

    I just gave it a spin on a local install where it worked - I also managed to get it working using @Umbraco.Field like this

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        var selection = Model.Content.Site().FirstChild("blog").Children("blogItem").OrderBy("CreateDate desc").Take(3);
    foreach(var item in selection){
      <div class="recent-blog-div">
          <a href="@item.Url" class = "main-page-blog-title-links"><h4 class="recent-blog-title">@item.GetPropertyValue("blogPostShortTitle")</h4></a>
          <div class="text-block-3">@Umbraco.Field(item, "createDate", formatAsDate: true)</div>
       </div>
     }
    }
    

    Be aware that the date formatting is different in the two examples though. You can see Jeavon's example here to control the date formatting more if you go with the first example https://our.umbraco.org/forum/developers/razor/44889-Formatting-Date-#comment-161445

    I hope this helps!

    /Jan

  • Matt 353 posts 825 karma points
    Jun 08, 2018 @ 14:16
    Matt
    0

    Hello Jan,

    That worked a treat thanks all, Can I ask why I needed to add Item in front of it?

    @Umbraco.Field(item, "createDate", formatAsDate: true)
    

    Just so I get a better understanding of how things work :)

    Thanks

  • Harry Spyrou 212 posts 604 karma points
    Jun 08, 2018 @ 14:31
    Harry Spyrou
    0

    You're in a foreach loop.

    when you do:

    @foreach (var BLUBLU in my.collection.in.Umbraco) { //this no longer exists. Model.Content (or Umbraco.Field in your case) means that the code thinks that you're on the same page (Model.Content/Umbraco.Field = CurrentPage

  • @Model.Content.GetPropertyValue('createDate')
  • This now is valid. Instead of currentPage you now are in a loop and your node is the variable BLUBLU (or 'item' in your case). This will be valid now:

    BLUBLU.GetPropertyValue('createDate')

    }

    Furthermore, try using UmbracoViewPage instead of UmbracoTemplatePage as they will be removing it in future versions of Umbraco.

    It's also simpler. With UmbracoViewPage you only have to type Model. instead of Model.Content.

    One more thing. You could just do something as simple as Model.Content.CreateDate to get the built in Create Date. Or in your case item.CreateDate.

Copy Link
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 08, 2018 @ 14:25
    Jan Skovgaard
    1

    Hi Matt

    Yeah, it's not really documented anywhere - Yet. I made a Pull Request for the documentation yesterday to provide an example.

    The reason why you can't access it without using the "item" is because you need to add the context to the method so it knows where to get the data from. So you will need to provide that when you're either in a foreach loop context or if you're inside a partial where you will need to passe "CurrentPage" for instance.

    I hope this makes sense.

    /Jan

    Copy Link
  • Please Sign in or register to post replies

    Write your reply to:

    Draft