2017-08-11 148 views
0

我想在網站的每個頁面上顯示隨機引號。如果我嘗試使用Content,它可以工作,但每個模塊都有自己的內容。當我切換到使用數據時,我似乎無法隨機選擇列表中的項目。我有一個內容類型爲'Quotation'的只有一個字段,名爲'Quotation'。如何從2sxc數據流中選擇一個隨機項目?

下面是一個使用作品內容測試:

<div> 
<h2>@Dnn.Module.ModuleTitle</h2> 
<div class="sc-element">Add more items to the list and see which one is selected at [email protected](ListContent)</div> 
@{ 
Random rnd = new Random(); 
int r = rnd.Next(List.Count); 
int rp1 = r + 1; 
} 
<div class="sc-element">Number @rp1, <i>@List[r].Content.Quotation</i>, was selected from this list of @List.Count items: </div> 
<ol> 
@foreach(var e in List) { 
    var Content = e.Content; 
    <li class="sc-element"> 
     @Edit.Toolbar(Content) 
     @Content.Quotation 
    </li> 
} 
</ol> 

如果我試圖用數據流類似的事情,我不能隨意訪問列表項。下面的代碼:

@using ToSic.Eav.DataSources 
<div class="BasicContentWithPreview sc-element"> 
    @Edit.Toolbar(ListContent) 
    <h2><span>Random: @Dnn.Module.ModuleTitle</span></h2> 
    @{ 
     var allQuotes = CreateSource<EntityTypeFilter>(); 
     allQuotes.TypeName = "Quotation"; 
    } 
    @{ 
     var ql = allQuotes.List; 
     Random rnd = new Random(); 
     int r = rnd.Next(ql.Count); 
     int rp1 = r + 1; 
    } 
    <div class="sc-element">Number @rp1, <i>@AsDynamic(ql[r].Quotation)</i>, was selected from this list of @ql.Count items: </div> 
    <ol> 
     @foreach (var q in ql) 
     { 
      var item = AsDynamic(q.Value); 
      <li class="sc-element">[@item.EntityId] @item.Quotation</li> 
     } 
    </ol> 
</div> 

錯誤說:

「ToSic.Eav.IEntity」不包含「報價」的定義,並沒有擴展方法「報價」接受一個類型的第一個參數「ToSic.Eav.IEntity」可以發現

當我刪除了ql[r]部分,所有的工作,而列表底部是完美的。所以我可以在列表中順序訪問該項目,但無法隨機訪問它們。我嘗試了很多組合。任何想法如何正確地做到這一點?謝謝。

回答

0

有一個洗牌的數據源中2sxc 8.10開始 - http://2sxc.org/en/blog/post/releasing-2sxc-8-10-public-rest-api-visual-query-and-webapi-shuffle-datasource

可以在視覺查詢使用,或使用CreateSource < ...>使用它。這應該爲你做

你也可以使用LINQ語句,對於你可以看看洗牌,看看它是如何工作https://github.com/2sic/eav-server/blob/master/ToSic.Eav.DataSources/Shuffle.cs

希望這有助於,如果是,請標記爲回答:)

相關問題