1

I am writing a foreach loop where I am outputting some HTML. I need to change the name of an input element each time it goes through the loop. I know in PHP it is super easy to combine text with a PHP variable. I cannot figure out how to do this in C#. I am using MVC3 Razor.

<img src="@Url.Content("~/Content/img/subtract.png")" alt="Subtract" class="qtyminus" field="qtyValue@i" />

All this code does is print out "qtyValue@i". It does not interpret the value of @i.

0

4 Answers 4

2

You could try using parentheses around the variable you're trying to output.

<img src="@Url.Content("~/Content/img/subtract.png")" alt="Subtract" class="qtyminus" field="qtyValue@(i)" />
Sign up to request clarification or add additional context in comments.

Comments

1
<img src="@Url.Content("~/Content/img/subtract.png")" alt="Subtract" class="qtyminus" field="qtyValue@(i)" />

Comments

0

you will not have the expected output as it's considered as a string try this

<img src="@Url.Content("~/Content/img/subtract.png")" alt="Subtract" class="qtyminus" field="qtyValue"+ @i />

Comments

0

You are looking for:

<img src="@Url.Content("~/Content/img/subtract.png")" alt="Subtract" 
class="qtyminus" 
[email protected](@"""qtyValue{0}""",i) />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.