html
  • css
  • asp.net
  • asp.net-mvc-4
  • razor
  • 2016-04-28 45 views 2 likes 
    2

    目前,此刻我是从一个循环和上面的文字使用i class和下面的代码红圈显示器能够显示文本格式:使用循环

    <div id="testwrap"> 
    @foreach (mainnav mainnav in @Model) 
    { 
        if (mainnav.HasSideNav == 0) 
        { 
        <a style="color:red" href='@Url.Action("Content", new { id = mainnav.contentID })' class="ajax2">@mainnav.DisplayLabel</a> 
        } 
        else 
        { 
         <div class="col-sm-3 text-center feature"> 
         <i class="fa fa-tablet iconred"></i> 
        <a style="color:blue" href='@Url.Action("SideNavLevel1", new { id = mainnav.MNavSubID })' class="ajax2">@mainnav.DisplayLabel</a> 
        </div> 
        } 
    } 
    </div> 
    

    我的目标是有图标是蓝色的,绿色,黄色,橙色等...

    我试图添加一个计数器,但它不工作。

    任何人都可以帮助我添加循环/计数器来改变iconred到iconblue等的位置吗?

    +0

    你想要什么时候红色,橙色或蓝色? – Shyju

    +0

    我有4个圈出现,所以它应该是iconred,iconblue,iconyellow,icongreen。问题是我正在使用一个循环从表格中获取这些数据,并将其作为一条线上的4个链接返回,这就是为什么我只得到四个红色圆圈。 – TerrorTot38

    +0

    那么第五个项目会再次变成红色? – Shyju

    回答

    3

    您可以将您的颜色保存在一个数组中,并与计数器一起使用。

    @{ 
        var counter = 0; 
        string[] colors = new string[] { "red", "blue", "orange","green" }; 
    } 
    
    <div id="testwrap"> 
    
        @foreach (var mainnav in YourCollection) 
        { 
         if (mainnav.HasSideNav == 0) 
         { 
          <a style="color: red" href='#' class="ajax2">@mainnav.DisplayLabel</a> 
         } 
         else 
         { 
          <div class="col-sm-3 text-center feature"> 
           <i class="fa fa-tablet [email protected](colors[counter])"></i> 
           <a style="color: blue" href='#' class="ajax2">@mainnav.DisplayLabel</a> 
          </div> 
         } 
         counter++; 
         if (counter == 4) 
         { 
          counter = 0; 
         }  
        } 
    </div> 
    

    如果您的收藏中有4个以上的物品,第五个物品会再次变成红色等等。

    +0

    谢谢,我在计数器代码和图标@(颜色[计数器]中添加了它,它工作:) – TerrorTot38

    相关问题