Conditionally change CSS class in Razor view

前端 未结 4 946
别那么骄傲
别那么骄傲 2020-11-30 09:31

I need to change the CSS class of the

tag with the \'forumChild\' class. It has to change every 3 loops of the foreach loop.

Is there a way

4条回答
  •  青春惊慌失措
    2020-11-30 10:18

    You can add a counter variable to will start with 1 and increment in loop. Check with if statement is true by % and change the class name

    @{ int counter = 1;}
    
     @foreach (var item in Model)
    {
    
    if( (counter % 3) ==0 )
    {
     
    } else {
    } i++;

提交回复
热议问题