Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding css class to a div with jquery inside an MVC foreach loop
    primarykey
    data
    text
    <p>As the title states.</p> <p>i am trying to add headers based on certain values within my model.</p> <p>currently it looks something like this.</p> <pre><code>foreach (var track in Model) { if (track.G1) { &lt;script type="text/javascript"&gt; jQuery(".model-attribute-wrapper").addClass("G1"); &lt;/script&gt; } else if (track.Handicap) { &lt;script type="text/javascript"&gt; jQuery(".model-attribute-wrapper").addClass("handicap"); &lt;/script&gt; } else if (track.Special) { &lt;script type="text/javascript"&gt; jQuery(".model-attribute-wrapper").addClass("special"); &lt;/script&gt; } else if (track.BestRight) { &lt;script type="text/javascript"&gt; jQuery(".model-attribute-wrapper").addClass("bestright"); &lt;/script&gt; } else if (track.Dirt) { &lt;script type="text/javascript"&gt; jQuery(".model-attribute-wrapper").addClass("dirt"); &lt;/script&gt; } if (even) { even = false; &lt;div class="model-attribute-wrapper even"&gt; &lt;div class="model-attribute xx-small"&gt;@Html.DisplayFor(modelItem =&gt; track.Id) &lt;/div&gt; &lt;div class="model-attribute huge"&gt;@Html.DisplayFor(modelItem =&gt; track.Name) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Handicap) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Special) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Dirt) &lt;/div&gt; &lt;div class="model-attribute medium"&gt;@Html.DisplayFor(modelItem =&gt; track.BestRight) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.G1) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Distance) &lt;/div&gt; &lt;div class="model-attribute action"&gt; &lt;div class="model-action"&gt;@Html.ActionLink("Edit", "Edit", new { id = track.Id }) &lt;/div&gt; &lt;div class="model-action"&gt;@Html.ActionLink("Delete", "Delete", new { id = track.Id }) &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; } else { even = true; &lt;div class="model-attribute-wrapper odd"&gt; &lt;div class="model-attribute xx-small"&gt;@Html.DisplayFor(modelItem =&gt; track.Id) &lt;/div&gt; &lt;div class="model-attribute huge"&gt;@Html.DisplayFor(modelItem =&gt; track.Name) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Handicap) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Special) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Dirt) &lt;/div&gt; &lt;div class="model-attribute medium"&gt;@Html.DisplayFor(modelItem =&gt; track.BestRight) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.G1) &lt;/div&gt; &lt;div class="model-attribute small"&gt;@Html.DisplayFor(modelItem =&gt; track.Distance) &lt;/div&gt; &lt;div class="model-attribute action"&gt; &lt;div class="model-action"&gt;@Html.ActionLink("Edit", "Edit", new { id = track.Id }) &lt;/div&gt; &lt;div class="model-action"&gt;@Html.ActionLink("Delete", "Delete", new { id = track.Id }) &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; } } </code></pre> <p>which does not seem to be working as it adds a ton of classes to the first 2 rows then it gradually seems to not add one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload