user1823761
user1823761

Reputation:

How to use for loop in ASP.NET MVC?

I have a very basic ASP.NET MVC project, that uses C# for the programming language and Razor for its view engine. I want to generate this HTML markup with for loop in the view.

<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<div class="item-3">Item 3</div>
<div class="item-4">Item 4</div>
<div class="item-5">Item 5</div>
...
<div class="item-N">Item N</div>

— Where N is a defined number in the loop.

How can I do this?

Upvotes: 5

Views: 17222

Answers (2)

Nomad101
Nomad101

Reputation: 1698

use something like this

@for(int i = 0; i <= N; i++)
{
    <div class="item-@i"> Item @i </div>
}

Take a look at this for a refence to how to us the @ symbol in Razor markup. Razor cheat sheet

Upvotes: 4

rpgmaker
rpgmaker

Reputation: 790

@for(var i = 1; i <= 5; i++){
   <div class="item-@i">Item @i</div>
}

Upvotes: 9

Related Questions