ksmsk
ksmsk

Reputation: 37

Looping Through Properties and accessing its own properties

I am new to C# and Umbraco. Can't figured this simple thing out nor find the solutions

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}
List<string> test = new List<string>();
var item = CurrentPage.Children.FirstOrDefault();
var count = 0;
foreach (var prop in item.Properties) {
    count++;
    test.Add(prop.PropertyTypeAlias);
}
<html>
...
</html>

If i count properties it gives correct number of properties but i can't get access its own properties such as "Value", "PropertyTypeAlias" etc. It throw exception reads: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'PropertyTypeAlias'

Thanks in advance.

Upvotes: 0

Views: 158

Answers (1)

Mikkel
Mikkel

Reputation: 1865

If you want to access properties of a document type, it's better and easier to do it like this:

@inherits UmbracoTemplatePage
@{

}

<h1>@Model.Content.Name</h1>
@foreach (var item in Model.Content.Children)
{
    <h2>@(item.GetPropertyValue<IHtmlString>("bodyText"))</h2>
}

CurrentPage is a dynamic object, whereas Model.Content gets the "current page", but as a strongly-typed IPublishedContent, which means, if you use Visual Studio, you get intellisense and can see which methods you can use.

In the H1-tag, I simply pulled out the current content node's name, in the foreach, I am looping over the current content node's children and displaying their property with alias "bodyText" and datatype "Rich text".

EDIT:

@inherits UmbracoTemplatePage
@{
    var child = Model.Content.Children.FirstOrDefault();
    var properties = new List<string>();
    foreach (var item in child.Properties)
    {
        properties.Add(item.PropertyTypeAlias);
    }
}
@foreach (var item in properties)
{
    <h1>@item</h1>
}

Upvotes: 1

Related Questions