IPublishedContent and accessing content fields in Partial view
Hi,
I have a created Partial view for displaying the latest news item on the start page of my site. On the actual news page I use Currentpage in order to access all the news field and display them in a list. However, the start page wont work reusing the code so I tried a IPublishContent approach as shown below:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
<ul class="nyhetslista">
@*OrderBy() takes the property to sort by*@
@{
var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain");
if (locationNodes.Any())
{
foreach (var publishedContent in locationNodes.First().Children)
{
<p>@publishedContent.Id</p>
foreach (var page in publishedContent.Children)
{
<p>@page.Id</p>
}
}
}
}
</ul>
@page.Id works fine, but I cant figure out how to reference the actual content fields. For example, if I try @page.huvudrubrik (where "huvudrubrik" is the alias of a textfield in the content page), I get this error message:
Compiler Error Message: CS1061:
'Umbraco.Core.Models.IPublishedContent' does not contain a definition
for 'Huvudrubrik' and no extension method 'huvudrubrik' accepting a
first argument of type 'Umbraco.Core.Models.IPublishedContent' could
be found (are you missing a using directive or an assembly reference?)
Can somebody help me?
Best regards, /Martin
Below is the working partial view for the news page if it could be of any help in understanding my problem:
<ul class="nyhetslista"> @*OrderBy() takes the property to sort by*@
@{ var locationNodes = Model.AncestorOrSelf(1).Descendants("NyheterMain"); if (locationNodes.Any()) { foreach (var publishedContent in locationNodes.First().Children) {
<p>@publishedContent.Id</p> foreach (var page in publishedContent.Children) { <p>@page.GetPropertyValue<string>("huvudrubrik")</p>
Also Martin, from your first code example, I notice you have two inherits statements. From what I know, I think your view can only inherit from one. The last being used in your first example. It might be worth checking to see if thats correct.
When adding <p>@page.GetPropertyValue<string>("huvudrubrik")</p> I get this error:
Compilation Error Description: An error occurred during the
compilation of a resource required to service this request. Please
review the following specific error details and modify your source
code appropriately.
Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)'
has some invalid arguments
Source Error:
Line 20: foreach (var page in publishedContent.Children)
Line 21: { Line 22:
@page.GetPropertyValue
Line 23: Line
24: }
I also added @using Umbraco.Web;but no change. It seem strange, as the content page node Id is displayed correctly, the page fields should be available too...
It didn't unfortunately, I still get this error, where line 10 is red:
Compilation Error Description: An error occurred during the
compilation of a resource required to service this request. Please
review the following specific error details and modify your source
code appropriately.
Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)'
has some invalid arguments
Source Error:
Line 8:
@publishedContent.Id
Line 9:
foreach (var page in publishedContent.Children){ Line 10:
@page.GetPropertyValue
Line 11:
} Line 12:
}
Do you know if it possible to rewrite my "Currentpage" partial in a way so that Currentpage is replaced with the proper content page instead? In order to get a non-dynamic reference.
Thanks for your effort but I got a different error. It says that @ is not needed as I have declared a code section on the 3rd row and havn't left that section yet.
Sorry to say it didn't help, I got a different error complaining about "Content". I think I have to look into a different approach to make this work. If I'll work it out, I'll let you know.
If the news is different on the start page why do you want to use a partial view? Can't you just do your coding on the homepage template instead of a partial view?
It might also help to check out the Hybrid Framework. It has some best practises for Umbraco 7.
It found a nice solution in the TXT Starter Kit. The trick was to use Currentpage and reference doc type in plural (i.e. doc type name + "s"). Thanks once again for all suggestions!
// Best regards Martin.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var homePage = CurrentPage.AncestorsOrSelf(1).First();
var newsOverview = homePage.NyheterMains.First();
var newsItems = newsOverview.EnskildNyhets.OrderBy("publishDate desc, createDate desc").Take(3); }
<ul class="nyhetslista">
@foreach (var newsItem in newsItems)
{
// If the editor has not explicitly provided the "Page title" property page
// then just show the name of the page otherwise show the provided title
var title = string.IsNullOrWhiteSpace(newsItem.Title)
? newsItem.Name
: newsItem.Title;
// If the editor has not explicitly set the publishDate property then show the create date
var dateTime = newsItem.PublishDate == default(DateTime)
? newsItem.CreateDate
: newsItem.PublishDate;
<li>
<article class="">
<h3><a href="@newsItem.Url">@newsItem.huvudrubrik</a></h3>
<ul>
<li>@newsItem.nyhetsdatum.ToString("d MMM yyyy")</li>
<li>@newsItem.huvudrubrik</li>
<li>@newsItem.underrubrik</li>
<hr class="line_gradient" /> <br> </ul>
</article>
</li>
} </ul>
IPublishedContent and accessing content fields in Partial view
Hi,
I have a created Partial view for displaying the latest news item on the start page of my site. On the actual news page I use Currentpage in order to access all the news field and display them in a list. However, the start page wont work reusing the code so I tried a IPublishContent approach as shown below:
@page.Id
works fine, but I cant figure out how to reference the actual content fields. For example, if I try@page.huvudrubrik
(where "huvudrubrik" is the alias of a textfield in the content page), I get this error message:Can somebody help me?
Best regards, /Martin
Below is the working partial view for the news page if it could be of any help in understanding my problem:
Hi Martin,
You will have to use the GetPropertyValue() method as follows in order to retrieve the correct property value:
Thanks, Dan.
Hi Martin,
What if you do something like this does this a difference.
If not then try to add @using Umbraco.Web;
Hope this helps,
/Dennis
Also Martin, from your first code example, I notice you have two
inherits
statements. From what I know, I think your view can only inherit from one. The last being used in your first example. It might be worth checking to see if thats correct.Thanks, Dan.
Unfortunately, it still does not work.
When adding
<p>@page.GetPropertyValue<string>("huvudrubrik")</p>
I get this error:I also added
@using Umbraco.Web;
but no change. It seem strange, as the content page node Id is displayed correctly, the page fields should be available too...Hi Martin,
What if you try something like this:
Hope this helps,
/Dennis
It didn't unfortunately, I still get this error, where line 10 is red:
Do you know if it possible to rewrite my "Currentpage" partial in a way so that Currentpage is replaced with the proper content page instead? In order to get a non-dynamic reference.
Hi Martin,
Think I found what goes wrong. We have missed the @ in front of the second for each loop
Hope this helps,
/Dennis
Thanks for your effort but I got a different error. It says that @ is not needed as I have declared a code section on the 3rd row and havn't left that section yet.
Hi Martin,
With this code I donĀ“t get any erros could you please see if it woks for you too.
/Dennis
Hi Dennis,
I didn't get any error but I didn't get any output either.
I call the partial using
@Html.Partial("NyheterFront", @Model.Content)
but I guess that passing@Model.Content
is correct.Hi Martin,
I'd try removing the second
@
character as it isn't necessary:Thanks, Dan.
Hi Martin,
And maybe you need to change the
To
Hope this helps,
/Dennis
Sorry to say it didn't help, I got a different error complaining about "Content". I think I have to look into a different approach to make this work. If I'll work it out, I'll let you know.
Thanks!
Hello,
If the news is different on the start page why do you want to use a partial view? Can't you just do your coding on the homepage template instead of a partial view?
It might also help to check out the Hybrid Framework. It has some best practises for Umbraco 7.
Jeroen
SOLVED IT!
It found a nice solution in the TXT Starter Kit. The trick was to use Currentpage and reference doc type in plural (i.e. doc type name + "s"). Thanks once again for all suggestions!
// Best regards Martin.
is working on a reply...