Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join text + variable in Razor

Tags:

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.

like image 724
Nitin Kabra Avatar asked Mar 01 '13 04:03

Nitin Kabra


People also ask

How do you join two string variables together?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

How do you declare a string variable in razor view?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

How do you join text in R?

To concatenate strings in r programming, use paste() function. The syntax of paste() function that is used to concatenate two or more strings. input strings separated by comma. Any number of strings can be given.


Video Answer


1 Answers

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."

like image 188
Alex Avatar answered Nov 09 '22 16:11

Alex