hmahdavi
hmahdavi

Reputation: 2354

How to add number into name attribute of html element in razor?

I try to add number into name attribute of html element in razor.

 @for (int j = 1; j <= numberOfTravelers; j++)
    {
<input type="text" name="FirstNameEN@j"  class="input-text full-width" value="" placeholder="First Name" />
}

In this case razor can not detect j number. How to fix this?

Upvotes: 0

Views: 217

Answers (2)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

Use @() as shown :-

@for (int j = 1; j <= numberOfTravelers; j++)
 {
    <input type="text" name="FirstNameEN@(j)" class="input-text full-width" value="" placeholder="First Name" />
 }

Upvotes: 2

Slava Utesinov
Slava Utesinov

Reputation: 13488

Try this:

@for (int j = 1; j <= numberOfTravelers; j++)
{
     <input type="text" name='@("FirstNameEN"+j)' class="input-text full-width" value="" placeholder="First Name" />
}

Upvotes: 1

Related Questions