循环前5项,然后创建ul并列出其余项

时间:2015-01-21 09:13:53

标签: c# razor umbraco

我试图在博客列表上创建某种排序功能。我想输出前5个项目,然后创建一个更多按钮,并列出其他类别以进行排序。

到目前为止我已经得到了这个:

@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/?@tag">@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>

2 个答案:

答案 0 :(得分: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/?@tag">@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/?@tag">@tag</a>
            </li>
       }
       usedTags.Add(tag);

    }
    </ul>
}
</ul>

}

答案 1 :(得分: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/?@tag">@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/?@tag">@tag</a>
                                </li>
                            }
                            usedTags.Add(tag);


                        }

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