Orchardcms 显示内容选择器字段中的容器零件项
我有一个名为DayTile的内容类型,我在其中设置了一个内容选择器字段,该字段仅限于HotelSection类型。此类型是Hotels类型的容器 我想在DayTile拾取该部分时渲染HotelSection中的所有酒店。这可能吗Orchardcms 显示内容选择器字段中的容器零件项,orchardcms,orchardcms-1.10,Orchardcms,Orchardcms 1.10,我有一个名为DayTile的内容类型,我在其中设置了一个内容选择器字段,该字段仅限于HotelSection类型。此类型是Hotels类型的容器 我想在DayTile拾取该部分时渲染HotelSection中的所有酒店。这可能吗 从我迄今为止的尝试来看,HotelSection包含的项目似乎无法作为内容选择器字段访问。如果我理解正确,这绝对是可能的 您需要覆盖主题/模块中的Fields.ContentPicker视图,并且在foreach循环中,您不需要显示每个HotelSection的链接,而
从我迄今为止的尝试来看,HotelSection包含的项目似乎无法作为内容选择器字段访问。如果我理解正确,这绝对是可能的 您需要覆盖主题/模块中的Fields.ContentPicker视图,并且在foreach循环中,您不需要显示每个HotelSection的链接,而是要显示该HotelSection内容项,该内容项将依次显示HotelSection中包含的所有酒店,如下所示:
@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;
@{
var field = (ContentPickerField) Model.ContentField;
string name = field.DisplayName;
var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
<span class="name">@name</span>
@if(contentItems.Any()) {
foreach(var contentItem in contentItems) {
@Display(contentItem.ContentManager.BuildDisplay(contentItem, "Detail"))
}
}
else {
<span class="value">@T("No content items.")</span>
}
</p>
@使用Orchard.ContentPicker.Fields
@使用Orchard.Utility.Extensions;
@{
变量字段=(ContentPickerField)Model.ContentField;
字符串名称=field.DisplayName;
var contentItems=field.contentItems;
}
@名字
@if(contentItems.Any()){
foreach(contentItems中的var contentItem){
@显示(contentItem.ContentManager.BuildDisplay(contentItem,“详细信息”))
}
}
否则{
@T(“无内容项”)
}
如果您希望从内容选择器字段中直接访问酒店,则需要在视图中添加更多内容,有点混乱,但如下所示:
@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;
@{
var field = (ContentPickerField) Model.ContentField;
string name = field.DisplayName;
var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
<span class="name">@name</span>
@if(contentItems.Any()) {
foreach(var contentItem in contentItems) {
var container = contentItem.As<ContainerPart>();
if(container == null) {
continue;
}
var hotels = contentItem.ContentManager
.Query(VersionOptions.Published)
.Join<CommonPartRecord>().Where(x => x.Container.Id == container.Id)
.Join<ContainablePartRecord>().OrderByDescending(x => x.Position);
foreach(var hotel in hotels) {
// do something
}
}
}
else {
<span class="value">@T("No content items.")</span>
}
</p>
@使用Orchard.ContentPicker.Fields
@使用Orchard.Utility.Extensions;
@{
变量字段=(ContentPickerField)Model.ContentField;
字符串名称=field.DisplayName;
var contentItems=field.contentItems;
}
@名字
@if(contentItems.Any()){
foreach(contentItems中的var contentItem){
var container=contentItem.As();
if(容器==null){
继续;
}
var hotels=contentItem.ContentManager
.Query(VersionOptions.Published)
.Join()。其中(x=>x.Container.Id==Container.Id)
.Join().OrderByDescending(x=>x.Position);
foreach(var hotel in hotels){
//做点什么
}
}
}
否则{
@T(“无内容项”)
}
我遗漏了顶部的一些使用语句,但要点就在那里。如果我理解正确,这绝对是可能的 您需要覆盖主题/模块中的Fields.ContentPicker视图,并且在foreach循环中,您不需要显示每个HotelSection的链接,而是要显示该HotelSection内容项,该内容项将依次显示HotelSection中包含的所有酒店,如下所示:
@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;
@{
var field = (ContentPickerField) Model.ContentField;
string name = field.DisplayName;
var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
<span class="name">@name</span>
@if(contentItems.Any()) {
foreach(var contentItem in contentItems) {
@Display(contentItem.ContentManager.BuildDisplay(contentItem, "Detail"))
}
}
else {
<span class="value">@T("No content items.")</span>
}
</p>
@使用Orchard.ContentPicker.Fields
@使用Orchard.Utility.Extensions;
@{
变量字段=(ContentPickerField)Model.ContentField;
字符串名称=field.DisplayName;
var contentItems=field.contentItems;
}
@名字
@if(contentItems.Any()){
foreach(contentItems中的var contentItem){
@显示(contentItem.ContentManager.BuildDisplay(contentItem,“详细信息”))
}
}
否则{
@T(“无内容项”)
}
如果您希望从内容选择器字段中直接访问酒店,则需要在视图中添加更多内容,有点混乱,但如下所示:
@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;
@{
var field = (ContentPickerField) Model.ContentField;
string name = field.DisplayName;
var contentItems = field.ContentItems;
}
<p class="content-picker-field content-picker-field-@name.HtmlClassify()">
<span class="name">@name</span>
@if(contentItems.Any()) {
foreach(var contentItem in contentItems) {
var container = contentItem.As<ContainerPart>();
if(container == null) {
continue;
}
var hotels = contentItem.ContentManager
.Query(VersionOptions.Published)
.Join<CommonPartRecord>().Where(x => x.Container.Id == container.Id)
.Join<ContainablePartRecord>().OrderByDescending(x => x.Position);
foreach(var hotel in hotels) {
// do something
}
}
}
else {
<span class="value">@T("No content items.")</span>
}
</p>
@使用Orchard.ContentPicker.Fields
@使用Orchard.Utility.Extensions;
@{
变量字段=(ContentPickerField)Model.ContentField;
字符串名称=field.DisplayName;
var contentItems=field.contentItems;
}
@名字
@if(contentItems.Any()){
foreach(contentItems中的var contentItem){
var container=contentItem.As();
if(容器==null){
继续;
}
var hotels=contentItem.ContentManager
.Query(VersionOptions.Published)
.Join()。其中(x=>x.Container.Id==Container.Id)
.Join().OrderByDescending(x=>x.Position);
foreach(var hotel in hotels){
//做点什么
}
}
}
否则{
@T(“无内容项”)
}
顶部缺少了一些using语句,但其要点就在那里。所选内容项由
ContentPickerField
sContentItems
属性公开(请参阅)
要从DayTile内容项访问此内容,假设从名为“content DayTile.cshtml”的Razor模板,您可以这样做:
@{
dynamic contentItem = Model.ContentItem; // How to access the content item depends on the context. In this case, we're in a Content shape template.
// Assuming your picker field is named "HotelSections" and attached to your DayTile content type (which in reality means it's attached to an automatically created part called "DayTile").
var picker = contentItem.DayTile.HotelSections;
var hotelSectionContentItems = picker.ContentItems; // Typed as IEnumerable<ContentItem>.
}
<ul>
@foreach(var hotelSectionContentItem in hotelSectionContentItems)
{
// You can now either access individual parts and fields of each hotel section content item and render it, or you can choose to build a shape for each item and display that. Here are examples:
<li>@hotelSectionContentItem.TitlePart.Title</li>
// or:
// Build a content shape for the content item.
var hotelSectionContentShape = BuildDisplay(hotelSectionContentItem, "Summary");
// Render the shape.
<li>@Display(hotelSectionContentShape)</li>
}
@{
dynamic contentItem=Model.contentItem;//如何访问内容项取决于上下文。在本例中,我们使用的是内容形状模板。
//假设选取器字段名为“HotelSections”,并附加到DayTile内容类型(实际上,这意味着它附加到自动创建的名为“DayTile”的部分)。
var picker=contentItem.DayTile.HotelSections;
var hotelSectionContentItems=picker.ContentItems;//类型为IEnumerable。
}
@foreach(hotelSectionContentItems中的var hotelSectionContentItem)
{
//现在,您可以访问每个hotel section内容项目的各个部分和字段并进行渲染,也可以选择为每个项目构建形状并进行显示。以下是示例:
- @hotelSectionContentItem.TitlePart.Title酒店
//或:
//为内容项生成内容形状。
var hotelSectionContentShape=BuildDisplay(hotelSectionContentItem,“摘要”);
//渲染形状。
- @显示(hotelSectionContentShape)
}
所选内容