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.
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.
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.
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.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With