Nitin Kabra
Nitin Kabra

Reputation: 3236

Join text + variable in Razor

In Razor how do i print some text + a variable value ?

eg :

@for(int i=0;i<5;i++)                      
{
  <input type="text" value="@i" id = "name_@i"/>
}

the above code prints id = "name_@i", but i want the value of i in the id tag.

Upvotes: 21

Views: 15088

Answers (2)

Kalpesh Dabhi
Kalpesh Dabhi

Reputation: 802

One simple way to insert variable in Razor view, insert variable at any where just wrap your variable to (XXX)

  <div data-original-title="user" data-toggle="tooltip" data-placement="top" class="@(identifier)completeicon"></div>
  <div data-original-title="user" data-toggle="tooltip" data-placement="top" class="comple@(identifier)teicon"></div>
  <div data-original-title="user" data-toggle="tooltip" data-placement="top" class="completeicon@(identifier)"></div>

Upvotes: 0

Alex
Alex

Reputation: 35409

Try the following:

@for(int i=0;i<5;i++)                      
{
  <input type="text" value="@(i)" id = "name_@(i)"/>
}

When you're having trouble getting Razor to understand your intent, use parenthesis around your expression to create an "Explicit Expression."

Upvotes: 39

Related Questions