2015-01-21 37 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

相关问题