Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally change CSS class in Razor view

I need to change the CSS class of the <div> tag with the 'forumChild' class. It has to change every 3 loops of the foreach loop.

Is there a way to do this from within the control?

<div class="Forum">
    <p>The Forum</p>

            @foreach (var item in Model)
            {               
                <div class="ForumChild">

                    <img src="@item.Blog.Image.img_path" alt="Not Found" />

                    <br />
                            @foreach (var comment in item.Blog.comment)
                            {

                                var db = new ACapture.Models.ACaptureDB();

                                var Name = from p in db.Profile.AsEnumerable()
                                           where (p.AccountID == comment.AccountID)
                                           select p;                            
                            <section>
                                    <div>
                                        <a href="@Url.Action("Index", "Home")">@foreach (var y in Name)
                                                                              { @(y.FirstName + " " + y.LastName + ":");
                                                                              }</a>
                                    </div>
                                    <div>
                                        @comment.Commentation 
                                    </div>
                            </section>
                            }
                </div>                
            }
</div>

Thanks in advance

like image 473
dright Avatar asked Aug 02 '12 20:08

dright


3 Answers

 @{
   int counter=0;
 }
 @foreach (var item in Model)
 { 
   counter++;
   <div class="@(counter<=3 ? "classRed":"classBlue")">
       <img src="@item.Blog.Image.img_path" alt="Not Found" />
       //other markup also here

   </div>  
    if (counter == 6)
    {
        counter = 0;
    }

 }

Where classRed and classBlue are the CSS classes

like image 106
Shyju Avatar answered Nov 04 '22 05:11

Shyju


How we handle this issue:

1) you need to create helper method that will return css class by some code.

string GetDivClass(int code)
{
  var classes = new [] {"first", "second", "third"};
  return classes[code];
}

2) create counter/index and increment it in the loop each time.

3) invoke helper method like GetDivClass(index % 3) at the div element.

PS

It is only POC, so don't use it in a real application (you need to add a validation logic and move 'classes' initialization to another place).

like image 23
user854301 Avatar answered Nov 04 '22 05:11

user854301


You can write any code you like into a Razor view, so to do what you're thinking of, you could do something like this (I left out most of the inner stuff):

@{
    var className = "ForumChild";
}
<div>
    @for (int i = 0; i < Model.Count; i++)
    {
        var item = Model[i];
        if (i % 3 == 0)
            className = GetNewClassName(); // Or whatever
        <div class="@className">
        </div>
    }
</div>
like image 2
Steve Czetty Avatar answered Nov 04 '22 07:11

Steve Czetty