2015-01-21 35 views
0

我試圖在博客列表上創建某種排序功能。我想輸出前5個項目,然後創建更多按鈕,並列出要從中排序的其他類別。循環前5項,然後創建ul並列出其餘的

我有了這個迄今:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage 
@{ 
var blogitems = Umbraco.Content("1102").Children.Where("Visible"); 
<ul> 
@foreach(var blog in blogitems) { 
    var tagsplit = blog.blogCats.Split(','); 
    var usedTags=new List<string>(); 
    foreach(var tag in tagsplit) { 

     //Output the first 5 items, then create a new <ul> and then list the rest 

     if(!usedTags.Contains(tag)){ 
      <li>      
        <a href="/blog/[email protected]">@tag</a> 
      </li> 
     } 
     usedTags.Add(tag); 
    } 
} 
</ul> 
} 

我希望這是有道理的?

到底想要的輸出應該是這個樣子:

<ul> 
       <li> 
        <a href="#" class="sort-item">Cat1</a> 
       </li> 
       <li> 
        <a href="#" class="sort-item">Cat2</a> 
       </li> 
       <li> 
        <a href="#" class="sort-item">Cat3</a> 
       </li> 
       <li> 
        <a href="#" class="sort-item">Cat4</a> 
       </li> 
       <li> 
        <a href="#" class="sort-item">Cat5</a> 
       </li> 
       <li> 
        <a href="#" class="sort-item dropdown-toggle" data-toggle="dropdown">More <b class="caret"></b></a> 
        <ul class="dropdown-menu"> 
         <li> 
          <a href="portfolio-1-col.html">Cat6</a> 
         </li> 
         <li> 
          <a href="portfolio-2-col.html">Cat7</a> 
         </li> 
         <li> 
          <a href="portfolio-3-col.html">Cat8</a> 
         </li> 
        </ul> 
       </li> 
      </ul> 
+1

'拿(5)'和「跳過(5)」? – MarcinJuraszek 2015-01-21 09:17:20

回答

0
@using System.Linq 
@using System.Xml.Linq 
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage 
@{ 
    var blogitems = Umbraco.Content("1102").Children.Where("Visible"); 
    <ul> 
     @foreach (var blog in blogitems) 
     { 
      var tagsplit = blog.blogCats.Split(','); 
      var usedTags = new List<string>(); 

      var i = 1; 
      foreach (var tag in tagsplit) 
      { 

       //Output the first 5 items, then create a new <ul> and then list the rest 
       if (i <= 5) 
       { 

        if (!usedTags.Contains(tag)) 
        { 
         <li> 
          <a href="/blog/[email protected]">@tag</a> 
         </li> 
        } 
        usedTags.Add(tag); 

       } 

       i += 1; 
      } 

      <li><a href="#" class="sort-item dropdown-toggle" data-toggle="dropdown">More <b class="caret"></b></a></li> 
      <ul> 

       @{ 
        i = 1; 
        foreach (var tag in tagsplit) 
        { 
         if (i > 5) 
         { 

          if (!usedTags.Contains(tag)) 
          { 
           <li> 
            <a href="/blog/[email protected]">@tag</a> 
           </li> 
          } 
          usedTags.Add(tag); 


         } 

         i += 1; 
        } 
       } 
      </ul> 
     } 
    </ul> 
+0

不,我總是返回5然後 - 所以它永遠不會輸出「更多」按鈕,而第二個ul – nuffsaid 2015-01-21 12:48:04

+0

我再次編輯我的答案。請參閱更改。 – 2015-01-21 12:56:30

2

你的代碼應該是這個樣子:

@using System.Linq 
@using System.Data.Linq 
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage 
@{ 
var blogitems = Umbraco.Content("1102").Children.Where("Visible"); 
<ul> 
@foreach(var blog in blogitems) { 
    var tagsplit = blog.blogCats.Split(',').ToList(); 
    var usedTags=new List<string>(); 
    foreach(var tag in tagsplit.Take(5)) { 

     //Output the first 5 items, then create a new <ul> and then list the rest 

     if(!usedTags.Contains(tag)){ 
      <li>      
        <a href="/blog/[email protected]">@tag</a> 
      </li> 
     } 
     usedTags.Add(tag); 
    } 


    <a href="#" class="sort-item dropdown-toggle" data-toggle="dropdown">More <b class="caret"></b></a> 
    <ul class="dropdown-menu"> 
    foreach(var tag in tagsplit.Skip(5)) { 

     if(!usedTags.Contains(tag)){ 
      <li>      
        <a href="/blog/[email protected]">@tag</a> 
      </li> 
     } 
     usedTags.Add(tag); 

    } 
    </ul> 
} 
</ul> 

}

+0

我得到這個錯誤:'System.Array'不包含'Take'的定義?它就像你不能使用foreach? – nuffsaid 2015-01-21 09:37:09

+1

請在您的Razor頁面頂部添加「@using System.Data.Linq」。應該管用!我也更新了我的答案,因此您可以在頁面頂部看到using指令。 – 2015-01-21 09:38:51

+0

或System.Linq,可能。 – Gerino 2015-01-21 09:39:40

相關問題