9

Lets say I have a for-loop to generate elements in a table:

@for (var i = 0; i < Model.Count(); i+=2) {
  <tr>
    @{var a = Model.ElementAt(i); var b = Model.ElementAtOrDefault(i + 1);}
    <td>
      <div id="r@i" class="rack-container">
        ...              
      </div>
      <div id="s@i" class="rack-selector fade">
        ...                   
      </div>
    </td>
    <td>
      @if (b != null) {
        <div id="r@i+1" class="rack-container">
          ...              
        </div>
        <div id="s@i+1" class="rack-selector fade">
          ...                   
        </div>
      }
    </td>
  </tr>
}

The problem is that the element is literally assigned "r@i" instead of "r1","r2"..."rN" and so on. Is there any way to combine text and a variable as an element ID?

1 Answer 1

14

Try:

<div id="s@(i + 1)" class="rack-selector fade">

It might be worth having a quick read of this.

Sign up to request clarification or add additional context in comments.

1 Comment

I just found stackoverflow.com/a/15278992/1200316 literally seconds after posting. Thank you, I promise I will try to look harder next time :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.