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.
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")
@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.
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.
Umbraco field not showing correct data
Hello,
Im having an issue with the following Umbraco fields;
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
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 usingGetPropertyValue
to retrieve values instead ofField
.Thanks,
Nik
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
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
Hi Matt,
When you say 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
Can you post your whole partial view please?
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
Hey Matt,
Try this:
Hello Nik,
Thanks for the reply.
This is what it displays out;
1/1/0001
Do you actually have a custom field called 'createDate' or are you just trying to get the CreateDate?
Hello Harry,
No custom field, I'm just trying to get the date of which the node was created by.
Thanks
Hi Matt
Can you please try this example?
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
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
Hello Jan,
That worked a treat thanks all, Can I ask why I needed to add Item in front of it?
Just so I get a better understanding of how things work :)
Thanks
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
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.
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
is working on a reply...