How to access item items when custom rendering a list in Orchard

I took over the rendering of the list in Orchard in order to be able to create my own markup (I need to create an interactive navigation to the actual contents of the list). This is the code I'm currently using:

@using Orchard.DisplayManagement.Shapes;

@{ var list = Model.List; var items = list.Items; var count = items.Count; var listTag = Tag(list, "ul"); listTag.AddCssClass("historia"); var index = 0; } <div class="produtos"> @foreach (var item in items) { var itemTag = Tag(item, "div"); itemTag.AddCssClass("item-" + index); @itemTag.StartElement; <h3> @item.Title </h3> <p>@item.MyCustomTextField</p> @itemTag.EndElement; ++index; } </div> @Display(Model.List) @Display(Model.Pager) 

It almost works ... I cannot access the fields of the list of elements ... I want to grab the heading and put it in h3 and then put the custom text field in the paragraph following it ... I will use CSS to display it correctly this part of the navigation. After this part of the navigation, I will call the standard list rendering procedure.

The problem is that @ item.Title gives the string null / empty. I want to access the contents of an element (no pun intended), which includes a custom text field, which is an excerpt for the entire message that will be displayed in the "navigation bar".

Bertrand shows us how to do this with blog posts:

 @using Orchard.ContentManagement; @{ IEnumerable<object> blogPosts = Model.ContentItems.ContentItems; } @if (blogPosts == null || blogPosts.Count() < 1) { <p>@T("No posts.")</p> } else { <ul class="content-items"> @foreach (dynamic post in blogPosts) { string title = post.Title; ContentItem item = post.ContentItem; <li class="content-item-summary"> @Html.ItemDisplayLink(title, item) </li> } </ul> } 

But it does not work the same with standard lists.

(Another small question: could / should I use a custom wrapper instead of overriding the entire layout for this list?)

+4
source share
1 answer

An example blog is very different. In the case of a list, what you have in the elements is shapes, not content elements. Now these forms often (but not always) have a link to the corresponding content element, usually under the ContentItem property. So, to get the name, for example, item.ContentItem.TitlePart.Title will provide you with what you want.

As for your packaging question, I donโ€™t know what you are trying to do, so I canโ€™t give any advice. If you want to maintain the integrity of the list, but surround it with additional markup, then yes, this is one way to do this.

+5
source

Source: https://habr.com/ru/post/1438480/


All Articles