I am unable to use @CurrentPage to get any properties from the page.
Normally I would make the model inherit from the render model like this:
using Umbraco.Web;
namespace MySite.Web.Models
{
public class ContactModel: Umbraco.Web.Models.RenderModel
{
public ContactModel()
: base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
{
}
}
}
The above would then allow me to use @Model.Content and I'd be able to access the properties that way.
The model I was using this time didn't allow for that. I didn't like the empty constructor.
I found a way of accessing the page's properties without needing to inherit from the render model.
The way I found was to add this to the top of the page:
@{
var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
}
I can now use @thisPage to access the page's properties the same as I would have done with @CurrentPage
I'm sure someone is going to come on here and tell us there is a much better way, but I see that as a good thing. At least we are discussing it.
I hope this post helps someone, if it does, please let me know.
Hi Paul,
Bit of a fan of yourself as you seem to be one of just a few in the Umbraco sharing your findings with everything especially in regard to Umbraco 8 changes.
Thanks
I been working on custom forms where I have a RenderAction to a surface controller which rendered the partial view for the form.
I needed to fetch current page data - like the associated email so depending on what "Store" you had the contact form on it would mail the right person the submitted data.
I too ran into this issue and was a bit lost about how to access the current page Model while already having the Model data for the form itself (Which also was built from watching one of your videos)
var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
Accessing CurrentPage properties in a partial which is using a custom model
Hi all I thought I would share this with you as when I was looking for something like this a while ago, I would have appreciated finding it.
When I have a partial which is declared like this:
I am unable to use
@CurrentPage
to get any properties from the page.Normally I would make the model inherit from the render model like this:
The above would then allow me to use
@Model.Content
and I'd be able to access the properties that way.The model I was using this time didn't allow for that. I didn't like the empty constructor.
I found a way of accessing the page's properties without needing to inherit from the render model.
The way I found was to add this to the top of the page:
I can now use
@thisPage
to access the page's properties the same as I would have done with@CurrentPage
I'm sure someone is going to come on here and tell us there is a much better way, but I see that as a good thing. At least we are discussing it.
I hope this post helps someone, if it does, please let me know.
Cheers
Paul
Hi Paul,
Did you try to use just:
Umbraco.AssignedContentItem
This is you actual current page, if you want strongly typed you have to use Model builders or something like that.
If you use this one
@inherits Umbraco.Web.Mvc.UmbracoViewPage
Try to access to data via @Model, without .Content
Thanks, Alex
Hi Alex I did try to use just
Umbraco.AssignedContentItem
but it didn't allow me to access the properties I had created for the page.Thats why this one worked better for me:
With regards to:
That doesn't work for me as it is getting the data from my Model, I want to get at the data from the content item.
Thanks for replying though.
Paul
Paul,
But this is - var thisPage = Umbraco.Content(this.Umbraco.AssignedContentItem.Id);
One more call to db фтв Umbraco.Content is dynamic, you have all data in the Umbraco.AssignedContentItem, just use
Umbraco.AssignedContentItem.GetPropertyValue
Thanks, Alex
If you need strongly typed Model use -
https://github.com/zpqrtbnk/Zbu.ModelsBuilder
I prefer to do it that way, but thanks for sharing yours.
Paul
Hi Paul, Bit of a fan of yourself as you seem to be one of just a few in the Umbraco sharing your findings with everything especially in regard to Umbraco 8 changes.
Thanks
I been working on custom forms where I have a RenderAction to a surface controller which rendered the partial view for the form.
I needed to fetch current page data - like the associated email so depending on what "Store" you had the contact form on it would mail the right person the submitted data.
I too ran into this issue and was a bit lost about how to access the current page Model while already having the Model data for the form itself (Which also was built from watching one of your videos)
This got me what I needed.
I've just been playing around with this and the best solution that I can see is a combination of the above posts:
This seems to be a much better option than inheriting from RenderModel.
Cheers, Tony
Thanks Paul, just used this method in Umbraco 10 and it's worked just fine:
is working on a reply...